Brandon Mintern wrote:
The same thing could be accomplished with:
class anonobject: pass a = anonobject() a.one = 1 a.two = 2
But there is really no easier way that I can think of to do this on the fly except to have already built a Record class as I previously indicated.
There is an "easier" way, for some definitions of "easy": a=type('anonobject', (object,), {})() # a is an instance of class anonobject a.one = 1 a.two = 2
Or if you want to keep it really short: a=type('anonobject', (object,), dict(one=1, two=2)) # a is a class named anonobject
I'll grant that this looks ugly, the intent of the code is far from obvious. But if you don't do this too often and place appropriate comments this should be fine. If you do find yourself doing this often, I would say writing an appropriate class is the best solution; it is certainly what I would have done in your example with the employees.
- Tal