What's going on here?

Gerald Klix Gerald.Klix at klix.ch
Wed Nov 22 15:53:38 CET 2006


Perhaps this piece of code might explain the behaviour:


 >>> class C( object ):
...     __slots__ = ()
...
 >>> o = C()
 >>> o.a = 1
Traceback (most recent call last):
   File "<input>", line 1, in ?
AttributeError: 'C' object has no attribute 'a'


object behaves like having an implict __slots__ attribute.


HTH,
Gerald


Dale Strickland-Clark schrieb:
> Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
> [GCC 4.1.0 (SUSE Linux)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>a = object()
>>>>a
> 
> <object object at 0xb7bbd438>
> 
>>>>a.spam = 1
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: 'object' object has no attribute 'spam'
> 
>>>>class b(object):
> 
> ...    pass
> ...
> 
>>>>a = b()
>>>>a
> 
> <__main__.b object at 0xb7b4dcac>
> 
>>>>a.spam = 1
>>>>
> 
> 
> What is subclassing adding to the class here? Why can't I assign to
> attributes of an instance of object?




More information about the Python-list mailing list