Cool object trick

Discussion in 'Python' started by dataangel, Dec 12, 2004.

  1. dataangel

    dataangel Guest

    I read some discussion on this list before about how sometimes it's
    useful to create a generic object class that you can just stick
    attributes to. I was reading the PyPanel source (not written by me) and
    I came across this:

    #----------------------------------------------------------------------------

    class Obj(object):
    #----------------------------------------------------------------------------

    """ Multi-purpose class """
    #----------------------------
    def __init__(self, **kwargs):
    #----------------------------
    self.__dict__.update(kwargs)

    Normally I'd just use class Obj(object): pass, but the advantage to this
    method is you can create an Obj like this:

    Obj(id="desktop", last=0, color=self.getColor(DESKTOP_COLOR))

    You can pass all the attributes you want the object to have this way.
    Nifty :)

    Sorry if this has been posted before, but I haven't seen it.
    dataangel, Dec 12, 2004
    #1
    1. Advertising

  2. dataangel wrote:
    > Normally I'd just use class Obj(object): pass, but the advantage to this
    > method is you can create an Obj like this:
    >
    > Obj(id="desktop", last=0, color=self.getColor(DESKTOP_COLOR))
    >
    > You can pass all the attributes you want the object to have this way.
    > Nifty :)


    Yup, that's basically what I was proposing in the pre-PEP:

    http://mail.python.org/pipermail/python-list/2004-November/252621.html

    I've let it slide for a bit here, but my intent is to make a patch that
    puts something like the pre-PEP class into the collections module and
    then send in the PEP. I've been kinda busy recently, and it's not quite
    trivial since Bunch is in Python and currently the collections module is
    in C, but hopefully in the next few weeks I'll have the time.

    Steve
    Steven Bethard, Dec 13, 2004
    #2
    1. Advertising

  3. dataangel

    Jive Guest

    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"]
    Jive, Dec 17, 2004
    #3
  4. dataangel

    has Guest

    Jive wrote:

    > # Is that really much different from this?


    Functionally, no. However it can help make code more readable when
    dealing with complex data structures, e.g. compare:

    obj.spam[1].eggs[3].ham

    to:

    obj["spam"][1]["eggs"][3]["ham"]

    I've used it a couple times for this particular reason and it
    definitely has its niche; though I'm not sure it's sufficiently common
    or useful to justify its inclusion it in the standard library, and it's
    trivial to whip up as-and-when it's needed.

    BTW & FWIW, I think the usual name for this kind of structure is
    'record' (or 'struct' in C).

    HTH

    has
    has, Dec 17, 2004
    #4
    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. RE: Cool object trick

    , Dec 17, 2004, in forum: Python
    Replies:
    4
    Views:
    263
    Kent Johnson
    Dec 17, 2004
  2. Alex Stapleton

    Re: Cool object trick

    Alex Stapleton, Dec 17, 2004, in forum: Python
    Replies:
    8
    Views:
    356
    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:
    103
    Michael 'entropie' Trommer
    Dec 28, 2005
  4. Tony Arcieri
    Replies:
    14
    Views:
    440
    Tony Arcieri
    Dec 9, 2010
  5. Tony Arcieri
    Replies:
    0
    Views:
    396
    Tony Arcieri
    Dec 14, 2010
Loading...

Share This Page