isinstance() bug

Skip Montanaro skip at pobox.com
Thu Jan 29 10:35:17 EST 2004


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

    Michal>  could you elaborate further why do you consider the current
    Michal>  behaviour to be correct? you've only described how it works
    Michal>  currently. i'd be interested what's the reasoning behind that.

It's not necessarily a bug.  More likely it's a feature.  It does what you
expect -- if your mental model of how things work corresponds to how things
really work. ;-)
 
    Michal>  i'm inclined to think that the current behaviour is a
    Michal>  side-effect of the implementation (perhaps to which some got
    Michal>  used to?).

Before Python had packages this sort of thing occurred much less frequently.
If CPython's behavior was changed now (I don't know if Jython's behavior is
the same) the change might raise subtle, hard-to-find errors.  Maybe you can
solve it by adding a shadow dictionary keyed by inode number.  It would
probably need to be exposed in sys as something like sys.module_inodes, so
that programmers can explicitly force module deletion from the runtime.  Any
place sys.modules is tweaked sys.module_inodes would have to be as well.

Skip





More information about the Python-list mailing list