Hi people,
I wonder why the isinstance() function is sensitive about the import
path i.e. the result depends not only on the class and the instance but
also on how a class is imported?
Example:
MyPackage/ Top-level package
__init__.py Initialize package
__me__.py Module used for setting Python-path
A.py Use objects of ForeignPackage and
subpackages
ForeignPackage/ Don't touch!
__init__.py Initialize package
B.py Defines class B1
MySubPackage/ Subpackage
__init__.py Initialize subpackage
C.py Defines instance checker for B1 instances
Provide some implementations:
__________________________________________________ ___________________________
# MyPackage.__init__.py
import __me__
__me__.setmypath()
# MyPackage.__me__.py
def setmypath():
import __me__
import inspect
import sys
absfile = inspect.getabsfile(__me__)
abspath =
"/".join(absfile.split("/")[:-1]+absfile.split("\\")[:-1])
sys.path.append(abspath)
# MyPackage.A.py
from MySubPackage.C import*
from ForeignPackage.B import*
b1 = B1()
checkInstance(b1)
#print b1.__class__
# MyPackage.ForeignPackage.__init__.py
# left empty
# MyPackage.ForeignPackage.B.py
class B1(object):
def __init__(self):pass
# MyPackage.MySubPackage.C.py
import MyPackage
import ForeignPackage.B as B
#import MyPackage.ForeignPackage.B as B
def checkInstance(inst):
if isinstance(inst,B.B1):
print "class is %s"%inst.__class__
else:
raise TypeError,"Instance of unknown class '%s'
found"%inst.__class__
__________________________________________________ __________________________
No run A.py :
A will call checkInstance with a B1 instance and prints
"class is <class 'MySubPackage.B.B1'>"
as expected.
But if one comments in the line
import MyPackage.ForeignPackage.B as B
in module C.py and calls A.py one gets:
Traceback (most recent call last):
....
TypeError: Instance of unknown class '<class 'ForeignPackage.B.B1'>'
found
The class which was expected by the checkInstance() function is
<class 'MyPackage.ForeignPackage.B.B1'>
I'm curios if someone could explain me the difference between this two
class objects?
Regards,
Kay 4 1507
Kay Schluehr wrote: I wonder why the isinstance() function is sensitive about the import path i.e. the result depends not only on the class and the instance but also on how a class is imported?
isinstance uses class object identity.
if you manage to import the same thing multiple times, you'll have
multiple class objects representing the same source code, and is-
instance won't work properly.
Example:
MyPackage/ Top-level package __init__.py Initialize package __me__.py Module used for setting Python-path A.py Use objects of ForeignPackage and subpackages ForeignPackage/ Don't touch! __init__.py Initialize package B.py Defines class B1 MySubPackage/ Subpackage __init__.py Initialize subpackage C.py Defines instance checker for B1 instances
in my newsreader, it looks like the C.py module is defined some-
where inbetween MyPackage and MyPackage/ForeignPackage.
what file system are you using? ;-)
(I don't have the energy to decipher your convoluted import
and path-manipulation code; but my intuition tells me that if
you flatten the hierarchy, put MyPackage and ForeignPackage
at the same level, and stop messing with the path inside the
__init__ files, your problems will just disappear).
</F>
Fredrik Lundh wrote: Kay Schluehr wrote:
I wonder why the isinstance() function is sensitive about the
import path i.e. the result depends not only on the class and the instance
but also on how a class is imported? isinstance uses class object identity.
if you manage to import the same thing multiple times, you'll have multiple class objects representing the same source code, and is- instance won't work properly.
Importing a class/module multiple times does not cause the problem.
Writing
import ForeignPackage.B as B1
import ForeignPackage.B as B2
in C.py does not destroy the identity B1 == B2. Perhaps it is recovered
from sys.modules["ForeignPackage.B"]? It is not actually isinstance()
that depends on path information but the module recovery. The importer
seems to disambiguate to greedy. Example:
MyPackage/ Top-level package __init__.py Initialize package __me__.py Module used for setting Python-path A.py Use objects of ForeignPackage and subpackages ForeignPackage/ Don't touch! __init__.py Initialize package B.py Defines class B1 MySubPackage/ Subpackage __init__.py Initialize subpackage C.py Defines instance checker for B1 instances in my newsreader, it looks like the C.py module is defined some- where inbetween MyPackage and MyPackage/ForeignPackage. what file system are you using? ;-)
This is surprisingly irrelevant. I used __me__.py for several projects
under Win2K without harm. Python seems to be more intelligent in path
handling than Windows.
(I don't have the energy to decipher your convoluted import and path-manipulation code; but my intuition tells me that if you flatten the hierarchy, put MyPackage and ForeignPackage at the same level, and stop messing with the path inside the __init__ files, your problems will just disappear).
Flattening the package structure would solve the problem as well as
destroying all file-system based packages and abandon them from Python.
A bad joke? Yes, but managing all code using a database instaed of an
OS dependent filesys would make sense to me.
Ciao,
Kay
Kay Schluehr wrote: if you manage to import the same thing multiple times, you'll have multiple class objects representing the same source code, and is- instance won't work properly. Importing a class/module multiple times does not cause the problem. Writing
import ForeignPackage.B as B1 import ForeignPackage.B as B2
that doesn't import it more than once; Python uses a cache to hold
modules, but the cache only works if Python can be sure that the
modules are the same thing, based on the information in the *import*
statement. in this case, "ForeignPackage.B" is clearly the same thing
as "ForeignPackage.B".
if you change the path around (in your case, by adding MyPackage to
the path, rather than relying on path-relative import), you'll introduce
multiple ways to import the same module, and the cache won't work
properly. and when the cache doesn't work, isinstance breaks down
for the reason I stated.
Flattening the package structure would solve the problem as well as destroying all file-system based packages and abandon them from Python.
well, packages for just fine for me. if you cannot get them to work,
maybe you should stop doing things that don't work.
</F>
Fredrik Lundh wrote: Kay Schluehr wrote:
if you manage to import the same thing multiple times, you'll
have multiple class objects representing the same source code, and is- instance won't work properly. Importing a class/module multiple times does not cause the problem. Writing
import ForeignPackage.B as B1 import ForeignPackage.B as B2
that doesn't import it more than once; Python uses a cache to hold modules, but the cache only works if Python can be sure that the modules are the same thing, based on the information in the *import* statement. in this case, "ForeignPackage.B" is clearly the same
thing as "ForeignPackage.B".
Yes, that's what I did expect.
if you change the path around (in your case, by adding MyPackage to the path, rather than relying on path-relative import), you'll
introduce multiple ways to import the same module, and the cache won't work properly. and when the cache doesn't work, isinstance breaks down for the reason I stated.
Exactly. "ForeignPackage.B" and "MyPackage.ForeignPackage.B" are two
paths pointing to the same module but it will be imported twice because
the cache does not lookup a strong name ( e.g. a hash of the module )
but the import path. This may also be the reason why it is not possible
to import a module in an interactive session, than edit it and finally
re-import it into the same running session. The hash value would have
been changed but the path remains the same. Flattening the package structure would solve the problem as well as destroying all file-system based packages and abandon them from
Python. well, packages for just fine for me.
In some cases they do, in other they don't. The __me__.py module comes
from working with ClearCase where the Python package is not installed
by an installation routine relative to an existing Python path. The
package has to recognize it's own position somewhere relative to the
virtual directory which can be arbitrary. Installing the package under
site-packages/ is a no go.
if you cannot get them to work, maybe you should stop doing things that don't work.
For shure, but people tend to do workarounds. So I did.
Regards,
Kay This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark Fisher |
last post by:
I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the...
|
by: Carlos Ribeiro |
last post by:
I am looking for information on packages & import hooks, including
simple examples on how to implement a simple import hook. Quick
googling turns out some documents, such as:
-- PEP 302:...
|
by: Maurice LING |
last post by:
Hi,
I think I've hit a system limit in python when I try to construct a list
of 200,000 elements. My error is
malloc: vm_allocate (size = 2400256) failed......
Just wondering is this...
|
by: Angelos Karantzalis |
last post by:
Hi y'all ...
I'm a bit puzzled here about .NET class instancing under COM+
Issue 1:
I've a COM+ component, let's call it ... COMDbWrapper that initializes
itself from an xml file.
The data...
|
by: Kent Lewandowski |
last post by:
hi all,
Recently I wrote some stored procedures using java jdbc code
(admittedly my first stab) and then tried to implement the same within
java packages (for code reuse). I encountered...
|
by: sanjeevdivekar |
last post by:
hi,
i am newbie to python so i am trying to learn mod_python as my new
development kit for my small web apps.
i am getting strange result can anybody explain me....
|
by: Zytan |
last post by:
I want multiple instances of the same .exe to run and share the same
data. I know they all can access the same file at the same time, no
problem, but I'd like to have this data in RAM, which they...
|
by: Alex Levinson |
last post by:
Hello -
I am a Perl programmer who's developing a Cocoa application with a development team. Our Cocoa-based application is going to execute some Perl scripts to take care of some systems...
|
by: koduruabhinav |
last post by:
Hi,
I have two files and each consists of modules and in turn modules consists of some verilog
data and instances.i have to search for module name in the two files and comapare the
contents...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |