isinstance() bug
Rainer Deyke
rainerd at eldwood.com
Thu Jan 29 12:50:42 EST 2004
Michal Vitecek wrote:
> Rainer Deyke wrote:
>> Michal Vitecek wrote:
>>> it's my belief that if i'm instantiating a class from the same
>>> location no matter how i imported its definition, its instances are
>>> always of the same class.
>>
>> Your belief is incorrect. 'package.module.A' and 'module.A' are
>> distinct classes. Modifications to one do not show up in the other.
>> If 'isinstance' reported that an instance of 'package.module.A' is
>> an instance of 'module.A', 'isinstance' would be broken and unusable.
>
> could you elaborate further why do you consider the current behaviour
> to be correct? you've only described how it works currently. i'd be
> interested what's the reasoning behind that.
'isinstance' does the only possible correct thing. Even without loading the
same file as multiple modules, a module can create many distinct classes
with the same name. For example:
def create_unique_class():
class C(object): pass
return C
a_bunch_of_classes = [create_unique_class() for i in range(8)]
Treating these separate classes as a single class would be just plain wrong.
If there's a problem with the current implementation, it is that it allows
modules in packages to be imported as top level modules.
--
Rainer Deyke - rainerd at eldwood.com - http://eldwood.com
More information about the Python-list
mailing list