Cool object trick

dataangel k04jg02 at kzoo.edu
Sun Dec 12 05:57:37 EST 2004


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.



More information about the Python-list mailing list