0

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?

Lord Kinboat
  • 47
  • 1
  • 2
  • 6

2 Answers2

3

AttributeError: 'NoneType' object has no attribute 'returngene'

This error happens when you try to access an attribute on the special None object. In your case you are trying to read the method returngene in order to call it. Clearly population[each] evaluates to None.

Your next step is to work out why population[each] evaluates to None. Presumably one of the items that you appended to population was None. And following that through we conclude that one of

chromosone.Chromosone(chromosoneSize)

or

chromosone.Chromosone.makeChromo(newOrganism)

returns None.

Now you know why this error occurs, you should be able to track down the root cause.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Probably you forgot to add a return statement to your makeChromo() method.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Giving makeChromo() a return statement gives me: TypeError: unbound method returngene() must be called with Chromosone instance as first argument (got nothing instead). But that's an entirely different error. Do I require a return statement for my @classmethod because it has to return the object it has created? – Lord Kinboat Mar 17 '12 at 09:27
  • @LordKinboat Of course. It only can return something if you tell it to do so. And no one can help you if you don't provide at least a minimal example... – glglgl Mar 17 '12 at 09:30
  • Apologies, I have a constructor that accepts a value as a length and randomly generates some bits. I needed another constructor so created a class method that accepts a string of bits, like so: @classmethod def makeChromo(cls, bits): obj = cls obj.gene = bits return obj – Lord Kinboat Mar 17 '12 at 09:50
  • Why don't you update your question in order to make that suffreadable? So I can only guess: if `cls` is a class to be instantiated, maybe you should do `obj = cls()`. Otherwise, it should work. – glglgl Mar 17 '12 at 09:53