I am a Python noob. To learn I’m making a natural selection simulator but I’m a bit stuck.
A bit of background:
I make a list of organisms with random bit patterns, like so:
population.append(chromosone.Chromosone(chromosoneSize))
Organisms breed, so I have a @classmethod to allow an organism to be created based on a combination of it’s parents bit patterns, like so:
population.append(chromosone.Chromosone.makeChromo(newOrganism))
At some points I return the gene from an organism, like so:
def returngene(self):
“””Return the gene”””
return self.gene
This works for organisms created by chromosone.Chromosone(chromosoneSize) but not for organisms created with chromosone.Chromosone.makeChromo(newOrganism). I get this error:
AttributeError: 'NoneType' object has no attribute 'returngene'
UPDATE: I have given my makeChromo() a return, like so:
@classmethod
def makeChromo(cls, bits):
obj = cls
obj.gene = bits
return obj
But I now get this error:
TypeError: unbound method returngene() must be called with Chromosone instance as first argument (got nothing instead)
returngene() is a simple method that returns the gene (a string).
I think my misunderstanding lies in the @classmethod and how Python works with types and objects?
This error happens when you try to access an attribute on the special
Noneobject. In your case you are trying to read the methodreturngenein order to call it. Clearlypopulation[each]evaluates toNone.Your next step is to work out why
population[each]evaluates toNone. Presumably one of the items that you appended topopulationwasNone. And following that through we conclude that one ofor
returns
None.Now you know why this error occurs, you should be able to track down the root cause.