Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!proxad.net!feeder1-2.proxad.net!usenet-fr.net!gegeweb.org!aioe.org!.POSTED!not-for-mail From: Marco Buttu Newsgroups: comp.lang.python Subject: Re: __bases__ misleading error message Date: Sat, 24 Jan 2015 22:51:32 +0100 Organization: Aioe.org NNTP Server Lines: 52 Message-ID: <54C413E4.5060208@gmail.com> References: <1a194e0a0b738d205de54180fa7@nntp.aioe.org> NNTP-Posting-Host: ygrj5FCXft6N9WbvUTST2g.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.python:84497 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