issubclass funny business

Fredrik Lundh effbot at
Tue Apr 11 18:23:38 CEST 2000

Les Schaffer <godzilla at> wrote:
> but when i need to test whther an object is an instance of a subclass
> of StandardArrayFileScanner:
>         # make sure std is of the correct type
>         if not issubclass(std.__class__, StandardArrayFileScanner):

how about:

        if not isinstance(std, StandardArrayFileScanner):

>             import cie
>             print std.__class__, cie.CIE.__bases__
>     raise NotStandardArrayError
> but it doesnt work, even though std is of type cie.CIE :
> (gustav)~/Engineering/dspring/stoplite/matlab/Jue-Data/: python
> cie.CIE (<class cie.CIE_Standards at 813b320>,)  <=== result of print stmt
> huh??????

you have two different StandardArrayFileScanner

when you run FileScanner as a script, it's evaluated
in the __main__ namespace.  when you import it, it's
evaluated again, in the FileScanner namespace.

solution: never import the script you use as the main

also see the last section on this page:


More information about the Python-list mailing list