RE: Cool object trick

Discussion in 'Python' started by Doran_Dermot@emc.com, Dec 17, 2004.

  1. Guest

    I rather like it! I prefer writing obj.spam to obj["spam"]! I wonder if
    there is a technical downside to this use of Python?

    P.S.

    Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon,
    obj.sausages, "and", obj.spam' a lot easier ;-)

    -----Original Message-----
    From: python-list-bounces+doran_dermot=
    [mailto:python-list-bounces+doran_dermot=] On Behalf Of
    Jive
    Sent: 17 December 2004 06:29
    To:
    Subject: Re: Cool object trick

    Kinda cool.

    It's occured to me that just about everything Pythonic can be done with
    dicts and functions. Your Obj is just a dict with an alternate syntax. You
    don't have to put quotes around the keys. But that's cool.


    class struct(object):
    def __init__(self, **kwargs):
    self.__dict__.update(kwargs)

    # Indented this way, it looks like a struct:
    obj = struct( saying = "Nee"
    , something = "different"
    , spam = "eggs"
    )

    print obj.spam

    # Is that really much different from this?

    obj2 = { "saying" : "Nee"
    , "something" : "different"
    , "spam" : "eggs"
    }

    print obj2["spam"]


    --
    http://mail.python.org/mailman/listinfo/python-list
     
    , Dec 17, 2004
    #1
    1. Advertising

  2. Steve Holden Guest

    wrote:

    > I rather like it! I prefer writing obj.spam to obj["spam"]! I wonder if
    > there is a technical downside to this use of Python?
    >
    > P.S.
    >
    > Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon,
    > obj.sausages, "and", obj.spam' a lot easier ;-)
    >

    Of course this whole thing of substituting attribute access for
    dictionary keys only works as long as the keys are strings with the same
    syntax as Python identifiers, so one shouldn't go completely overboard.
    "To the man with a hammer everything looks like a nail", and so on.

    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
     
    Steve Holden, Dec 17, 2004
    #2
    1. Advertising

  3. Steve Holden wrote:

    >> Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon,
    >> obj.sausages, "and", obj.spam' a lot easier ;-)
    >>

    > Of course this whole thing of substituting attribute access for dictionary keys only works as long
    > as the keys are strings with the same syntax as Python identifiers, so one shouldn't go completely
    > overboard.


    unless you're willing to use getattr() for thos oddball cases, of course.

    >>> class Dummy:

    ... pass
    ...
    >>> x = Dummy()
    >>> setattr(x, "spam&egg", "hello")
    >>> getattr(x, "spam&egg")

    'hello'
    >>> x.spam&egg

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: Dummy instance has no attribute 'spam'

    but seriously, turning container elements into attributes should only be done
    if it makes sense from a design perspective. (and vice versa; you shouldn't
    use a dictionary if an object would make more sense -- but attribute abuse
    is a lot more common)

    </F>
     
    Fredrik Lundh, Dec 17, 2004
    #3
  4. Steve Holden Guest

    Fredrik Lundh wrote:

    > Steve Holden wrote:
    >
    >
    >>>Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon,
    >>>obj.sausages, "and", obj.spam' a lot easier ;-)
    >>>

    >>
    >>Of course this whole thing of substituting attribute access for dictionary keys only works as long
    >>as the keys are strings with the same syntax as Python identifiers, so one shouldn't go completely
    >>overboard.

    >
    >
    > unless you're willing to use getattr() for thos oddball cases, of course.
    >

    Of course.

    > >>> class Dummy:

    > ... pass
    > ...
    > >>> x = Dummy()
    > >>> setattr(x, "spam&egg", "hello")
    > >>> getattr(x, "spam&egg")

    > 'hello'
    > >>> x.spam&egg

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > AttributeError: Dummy instance has no attribute 'spam'
    >
    > but seriously, turning container elements into attributes should only be done
    > if it makes sense from a design perspective. (and vice versa; you shouldn't
    > use a dictionary if an object would make more sense -- but attribute abuse
    > is a lot more common)
    >

    Really we are talking about the outer limits here. Anyone preferring

    setattr(x, "spam&egg", "hello")

    to

    x["spam&egg"] = "hello"

    when it isn't necessary clearly doesn't share our two principal
    attributes: an elegant sense of design, fine knowledge of Python and an
    inherent modesty.

    Sorry: out *three* principal attributes. Bugger, I'll come in again.

    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
     
    Steve Holden, Dec 17, 2004
    #4
  5. Kent Johnson Guest

    Steve Holden wrote:
    > Really we are talking about the outer limits here. Anyone preferring
    >
    > setattr(x, "spam&egg", "hello")
    >
    > to
    >
    > x["spam&egg"] = "hello"
    >
    > when it isn't necessary clearly doesn't share our two principal
    > attributes: an elegant sense of design, fine knowledge of Python and an
    > inherent modesty.
    >
    > Sorry: out *three* principal attributes. Bugger, I'll come in again.


    LOL!

    Kent
     
    Kent Johnson, Dec 17, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dataangel

    Cool object trick

    dataangel, Dec 12, 2004, in forum: Python
    Replies:
    3
    Views:
    298
  2. Alex Stapleton

    Re: Cool object trick

    Alex Stapleton, Dec 17, 2004, in forum: Python
    Replies:
    8
    Views:
    373
    Fredrik Lundh
    Dec 18, 2004
  3. Michael 'entropie' Trommer

    cool way to save big object tree

    Michael 'entropie' Trommer, Dec 28, 2005, in forum: Ruby
    Replies:
    0
    Views:
    115
    Michael 'entropie' Trommer
    Dec 28, 2005
  4. Tony Arcieri
    Replies:
    14
    Views:
    466
    Tony Arcieri
    Dec 9, 2010
  5. Tony Arcieri
    Replies:
    0
    Views:
    415
    Tony Arcieri
    Dec 14, 2010
Loading...

Share This Page