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?