An empty object with dynamic attributes (expando)

Terry Reedy tjreedy at udel.edu
Fri Jun 4 23:13:32 EDT 2010


On 6/4/2010 10:25 PM, Alf P. Steinbach wrote:

>> 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
>
> You can add attributes to functions.
>
> I'm not sure, but I think it was you who once provided me with a
> reference to the relevant PEP (thanks!), so I guess it just slipped your
> mind on a late Friday night. :-)

Right on both counts. Function attributes are exceptions, and they were 
added after I learned Python.

> Regarding user defined classes, I gather that by "have them [attributes]
> disabled" you're including the case of a class with slots?

Yes. or custom __setattr__ or something.

tjr






More information about the Python-list mailing list