Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

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:

  • Could it be possible that the python solution is much faster?
  • If yes why?
  • are there better options in Perl to upgrade the easier interface?
  • Update: is Variable::Magic an option?

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found