issubclass funny business

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


Les Schaffer <godzilla at netmeg.net> 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
FileScanner.py
>
> cie.CIE (<class cie.CIE_Standards at 813b320>,)  <=== result of print stmt
>
> huh??????

you have two different StandardArrayFileScanner
classes.

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
program.

also see the last section on this page:

    http://www.pythonware.com/people/fredrik/fyi/fyi06.htm

</F>





More information about the Python-list mailing list