Absolultely confused...
Tim Peters
tim.peters at gmail.com
Thu Oct 6 12:09:47 EDT 2005
[Jeremy Moles]
> ...
> I may be missing something critical here, but I don't exactly grok what
> you're saying; how is it even possible to have two instances of
> PyType_vector3d? It is (like all the examples show and all the extension
> modules I've done in the past) a static structure declared and assigned
> to all at once, only once.
The most common cause is inconsistent import statements, with one
place using package-relative import but another place explicitly
specifying the package. Here's a simple pure Python example, with
this directory structure:
playground/
package_dir/
__init__.py
source.py
module.py
__init__.py is empty; it just serves to make `package_dir` a package.
source.py defines a single type, named Type:
class Type(object):
pass
module.py imports source.Type in two different ways, then prints various stuff:
from source import Type as one_way # package-relative import
from package_dir.source import Type as another_way
print one_way is another_way
print one_way.__name__, another_way.__name__
print repr(one_way), repr(another_way)
import sys
for k, v in sys.modules.items():
if "source" in k:
print k, v
Now _from_ playground, run module.py:
python playground/module.py
This is the output, with annotations; I ran this on Windows, so expect
to see backslashes <wink>:
False
That is, one_way is not the same object as another_way: there are two
distinct instances of the Type object.
Type Type
Although they're distinct, they have the same __name__, "Type".
<class 'source.Type'> <class 'package_dir.source.Type'>
Their reprs differ, though, showing the path via which they were imported.
source <module 'source' from 'C:\playground\package_dir\source.pyc'>
package_dir.source <module 'package_dir.source'
from 'C:\playground\package_dir\source.pyc'>
That's two lines of output from crawling over sys.modules: there are
two distinct instances of the entire `source` module. That's the real
cause of the multiple `Type` instances.
package-relative import is rarely a good idea. Are you doing that anywhere?
More information about the Python-list
mailing list