Python development: UserDict as object scaffolding
April 02, 2005 |
co.mments
I have a habit, when working in Python, of starting classes by extending UserDict, usually because I dont have a strong idea of where I'm going just yet. The UserDict acts as a scaffold. So I might start with something like this to fill out against the initial tests:
class ExchangeState(UserDict):
def __init__(self, msg_url='', exchange_url='', httplr_state=state_UNKNOWN):
UserDict.__init__(self)
self['msg_url']=msg_url # the URL of the message
self['httplr_state']=httplr_state # the current exchange state
self['exchange_url']=exchange_url # the HTTPLR exchange URL
As I'm working the code, some of the dict keys will get lifted to object fields:
class ExchangeState(UserDict):
def __init__(self, msg_url='', exchange_url='', mtype=None, httplr_state=state_UNKNOWN):
UserDict.__init__(self)
self['msg_url']=msg_url # the URL of the message
self['httplr_state']=httplr_state # the current exchange state
self['exchange_url']=exchange_url # the HTTPLR exchange URL
self['mtype']=mtype # the message mimetype
self.msg_url=msg_url # the URL of the message
self.state=httplr_state # the current exchange state
Eventually, the dict scaffolding will be taken away, leaving the object:
class ExchangeState:
def __init__(self, msg_url='', exchange_url='',mtype=None, httplr_state=state_UNKNOWN):
self.msg_url=msg_url # the URL of the message
self.state=httplr_state # the current exchange state
self.exchange_url=exchange_url # the HTTPLR exchange URL
self.msg_mtype=mtype # the message mimetype
Of course sometimes there is no lifting, and the class gets left as an extended dictionary. But I'm wondering, does anyone else develop classes like this? I'm finding it a very natural way of working.
April 2, 2005 03:37 PM
Comments
Never thought about it before, but it must one of the unconscious factors which make me happy when hacking in javascript: such scaffolding is the standard behaviour of ecmascript objects.
What are the benefits of doing this rather than just using object attributes from the start? It seems a bit odd to me. Is there something else you gain from UserDict?
"What are the benefits of doing this rather than just using object attributes from the start?"
Anthony, I have no idea; intellectually I know there's no real value over fields, it just feels right. Which is why I brought it up.
I just use dictionaries and plain old functions until I'm almost done.
Hmm... I'm pretty sure you are just weird. And after all, it's dictionaries all the way down anyway, self[...] = value or self.__dict__[...] = value... what's the difference? Just syntax, and a second copy of a dictionary. And a bunch of quotation marks which is what keeps me from doing this -- I'd rather use the occassional getattr than the constant ['attr'].
Post a comment
Trackback Pings
TrackBack URL for this entry:
http://www.dehora.net/mt/mt-tb.cgi/1523
Listed below are links to weblogs that reference Python development: UserDict as object scaffolding:
» Python antpattern: UserDict as object scaffolding from Bill de hÓra
In UserDict as object scaffolding I mentioned that: "I have a habit, when working in Python, of starting classes by extending UserDict, usually because I dont have a strong idea of where I'm going just yet." I've enough feedback to suggest this is eith... [Read More]
Tracked on April 9, 2005 08:17 PM