我是一個Python noob。要學習我正在做一個自然選擇模擬器,但我有點卡住了。Python'NoneType'對象查詢
一點背景:
我就與隨機位模式,像這樣的生物的列表:
population.append(chromosone.Chromosone(chromosoneSize))
生物品種,所以我有一個@classmethod
允許基於要創建一個有機體它的父母的結合位模式,就像這樣:
population.append(chromosone.Chromosone.makeChromo(newOrganism))
在某些點上,我從一個生物體返回基因,像這樣:
高清returngene(個體經營): 「」 「返回基因」 「」
回報self.gene
這適用於通過chromosone.Chromosone(chromosoneSize)
創造的生物,但不能用於chromosone.Chromosone.makeChromo(newOrganism)
創造生物。我得到這個錯誤:
AttributeError: 'NoneType' object has no attribute 'returngene'
更新:我已經給我的makeChromo()返回,就像這樣:
@classmethod
def makeChromo(cls, bits):
obj = cls
obj.gene = bits
return obj
但現在我得到這個錯誤:
TypeError: unbound method returngene() must be called with Chromosone instance as first argument (got nothing instead)
returngene( )是一個返回基因(一個字符串)的簡單方法。
我認爲我的誤解在於@classmethod
以及Python如何處理類型和對象?
'maheChromo()'返回什麼? – glglgl 2012-03-16 23:29:21
@glglgl:'None' :) – 2012-03-16 23:30:03
此外,這個詞是**染色體**而不是** chromosone ** – 2012-03-16 23:35:53