An empty object with dynamic attributes (expando)

Terry Reedy tjreedy at udel.edu
Fri Jun 4 21:01:23 EDT 2010


On 6/4/2010 8:01 PM, dmtr wrote:
>> Why does it have to be a one-liner? Is the Enter key on your keyboard
>> broken?
>
> Nah. I was simply looking for something natural and intuitive, like: m
> = object(); m.a = 1;
> Usually python is pretty good providing these natural and intuitive
> solutions.

As far as I can think of now, one cannot add attributes to *any* 
builtin-class instance, but can add attributes to any user class which 
does not have them disabled.

 >>> [].a = 3
Traceback (most recent call last):
   File "<pyshell#15>", line 1, in <module>
     [].a = 3
AttributeError: 'list' object has no attribute 'a'
 >>> class L(list): pass

 >>> i = L(); i; i.a = 3; i.a
[]
3

Terry Jan Reedy




More information about the Python-list mailing list