Christopher J. Bottaro wrote:
I find myself doing the following very often:
class Struct:
pass
...
blah = Struct()
blah.some_field = x
blah.other_field = y
...
Is there a better way to do this? Is this considered bad programming
practice? I don't like using tuples (or lists) because I'd rather use
symbolic names, rather than numeric subscripts. Also, I don't like having
to declare the empty Struct class everytime I want to do this (which is
very often).
I have a module of my own (data.py) that I use a lot. It contains:
class Data(object):
def __init__(self, **initial):
for name, val in initial.iteritems():
setattr(self, name, val)
def __repr__(self):
names = sorted([name for name in dir(self)
if not name.startswith('_')],
key=lambda name: (name.lower(), name))
return '%s(%s)' % (self.__class__.__name__, ', '.join([
'%s=%r' % (nm, getattr(self, nm)) for nm in names]))
The advantage is that I can see the value in the Data object simply
by printing the object. I use it like:
from data import Data
blah = Data(some_field=3, other_field=13)
...
blah.other_field = 23
...
--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list