As I write it, it seems almost surreal to me that I’m actually experiencing this problem.
I have a list of objects. Each of these objects are of instances of an Individual class that I wrote.
Thus, conventional wisdom says that isinstance(myObj, Individual) should return True. However, this was not the case. So I thought that there was a bug in my programming, and printed type(myObj), which to my surprise printed instance and myObj.__class__ gave me Individual!
>>> type(pop[0])
<type 'instance'>
>>> isinstance(pop[0], Individual) # with all the proper imports
False
>>> pop[0].__class__
Genetic.individual.Individual
I’m stumped! What gives?
EDIT: My Individual class
class Individual:
ID = count()
def __init__(self, chromosomes):
self.chromosomes = chromosomes[:] # managed as a list as order is used to identify chromosomal functions (i.e. chromosome i encodes functionality f)
self.id = self.ID.next()
# other methods
This error indicates that the
Individualclass somehow got created twice. You createdpop[0]with one version ofInstance, and are checking for instance with the other one. Although they are pretty much identical, Python doesn’t know that, andisinstancefails. To verify this, check whetherpop[0].__class__ is Individualevaluates to false.Normally classes don’t get created twice (unless you use
reload) because modules are imported only once, and all class objects effectively remain singletons. However, using packages and relative imports can leave a trap that leads to a module being imported twice. This happens when a script (started withpython bla, as opposed to being imported from another module withimport bla) contains a relative import. When running the script, python doesn’t know that its imports refer to theGeneticpackage, so it processes its imports as absolute, creating a top-levelindividualmodule with its ownindividual.Individualclass. Another other module correctly imports theGeneticpackage which ends up importingGenetic.individual, which results in the creation of the doppelganger,Genetic.individual.Individual.To fix the problem, make sure that your script only uses absolute imports, such as
import Genetic.individualeven if a relative import likeimport individualappears to work just fine. And if you want to save on typing, useimport Genetic.individual as individual. Also note that despite your use of old-style classes,isinstanceshould still work, since it predates new-style classes. Having said that, it would be highly advisable to switch to new-style classes.