I\'m wondering if Python has anything like the C# anonymous classes feature. To clarify, here\'s a sample C# snippet:
var foo = new { x = 1, y = 2 };
var bar
The type(...) form will fail the structural comparison requirement (without getting really ugly). The dict(...) form doesn't meet the attribute accessor requirement.
The attrdict seems to fall in the middle somewhere:
class attrdict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
a = attrdict(x=1, y=2)
b = attrdict(y=2, x=1)
print a.x, a.y
print b.x, b.y
print a == b
But it means defining a special class.
OK, I just noticed the update to the question. I'll just note that you can specify dict
for the bases parameter and only need to specify the constructor then (in the icky type expression). I prefer attrdict. :-)