Cool object trick
Jive
someone at microsoft.com
Fri Dec 17 01:28:39 EST 2004
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"]
More information about the Python-list
mailing list