Cool object trick

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dataangel

    Cool object trick

    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__.u pdate(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.getC olor(DESKTOP_CO LOR))

    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.
  • Steven Bethard

    #2
    Re: Cool object trick

    dataangel wrote:[color=blue]
    > 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.getC olor(DESKTOP_CO LOR))
    >
    > You can pass all the attributes you want the object to have this way.
    > Nifty :)[/color]

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



    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

    Comment

    • Jive

      #3
      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__.u pdate(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"]


      Comment

      • has

        #4
        Re: Cool object trick

        Jive wrote:
        [color=blue]
        > # Is that really much different from this?[/color]

        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

        Comment

        Working...