Cool object trick

D

dataangel

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.
 
S

Steven Bethard

dataangel said:
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
 
J

Jive

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"]
 
H

has

Jive said:
# 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top