On 24/01/2015 20:24, Terry Reedy wrote:

On 1/24/2015 5:16 AM, Mario Figueiredo wrote:

Consider the following code at your REPL of choice

        class Sub:
            pass

        foo = Sub()

        Sub.__bases__
        foo.__bases__

The last statement originates the following error:

This is an anomalous situation.  Normally, if a class has an attribute,
instances have the same attribute (unless overriden).  But this does not
matter.

That is not true: if a class has an attribute, you can not say its instances have the same attribute. You can just say if a type defines an attribute, all its instances have that attribute. Look at this example:

>>> class Foo(type):
...     foo = 33
...
>>> Foo.foo
33
>>> MyClass = Foo('MyClass', (), {})

MyClass is an instance of Foo, so it must have the attribute foo:

>>> isinstance(MyClass, Foo)
True
>>> MyClass.foo
33

But an instance of MyClass is not an instance of Foo, and so MyClass() must not have the attribute foo. In fact:

>>> m = MyClass()
>>> isinstance(m, Foo)
False
>>> m.foo
Traceback (most recent call last):
    ...
AttributeError: 'MyClass' object has no attribute 'foo'


--
Marco Buttu
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to