go ahead... be a heretic | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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
And returning a tied value could be used to extend it with a proper setter.
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
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
|
|