Dear monastery,

In my never ending desire to study different language designs I had a closer look into Python's OO model ...

... and I was very surprised to see that there attributes are accessed directly.

object.attribute = 10

is very common "pythonic" code.

Doing something like object->{attribute}=10 is not only very uncommon in Perl but also heavily frowned upon.

This is only partly because Perl's inner implementation is not necessarily a blessed hash and because in Perl methods and attributes have separated namespaces. (In python much like JS they are inside the same hash)

The main issue is that at the moment of object design you can't know how the nature of getting and setting attributes may evolve in the future...

... for instance you may later want to restrict allowed values to a range of values and the setter to act like a guard which can potentially report errors.

Could it be that the python community is totally ignorant of this issue?

Seems not, for this case they reserve a backdoor mechanism called property where a simple attribute value is replaced by another object with dedicated get, set, delete accessors. And to facilitate using it they provide a @property decorator as syntactic sugar. "@Decorators" are the python equivalent of ":attributes" (For deeper insights please see this SO discussion)

This reminds me a lot to the :lvalue technique once discussed for Perl.

We can easily define a getter mechanism for a attribute with

sub attribute :lvalue { return $value }

And returning a tied value could be used to extend it with a proper setter.

sub attribute :lvalue { return $tied_value }

This help provide a object->attribute = 10 interface, were the Store and Fetch of the tied value would act as getter and setters.

I remember seeing this discussed by Damian - not sure if here or in his OO book.

IIRC he dismissed this technique for being to slow.

Questions:

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

PS: this is a meditation seeking for insights... fanboys please spare me with "just use moose propaganda", I'll ignore it.


In reply to Language design: direct attribute access and postponed mutators (Perl Vs Python) by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.