How object's setattr differs from inherited?
Andrew Berg
bahamutzero8825 at gmail.com
Fri Jul 29 12:26:53 EDT 2011
On 2011.07.29 10:12 AM, Ciantic wrote:
>>>> class MyObject(object):
> ... pass
> ...
>>>> my = MyObject()
>>>> my.myvar = 'value' # No error!
>>>>
>>>> obj = object()
>>>> obj.myvar = 'value' # Causes error!
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'object' object has no attribute 'myvar'
>
> Why simple inheritance from object does not cause errors at setattr,
> yet direct init of object() does?
object objects have no __dict__. User-defined objects typically do. Each
child of MyObject would likely have __dict__ as well.
> I was trying to "decor" objects, lists etc with own attributes that
> could be used in templates and was sadly not permitted to do so:
>
>>>> something = [1,2]
>>>> something.myvar = 'value'
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'list' object has no attribute 'myvar'
Lists have no __dict__, and AFAIK, there's no reason they should.
--
CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
PGP/GPG Public Key ID: 0xF88E034060A78FCB
More information about the Python-list
mailing list