N
N
Nikita Sokolov2020-09-21 13:26:08
Python
Nikita Sokolov, 2020-09-21 13:26:08

Why does the NoneType error appear?

I just started learning python an hour ago) I don't understand the behavior of the compiler... I created a class:

class Specie:

    def __init__(self, x, y, z, t):
        self.x = x
        self.y = y
        self.z = z
        self.t = t
        self.fitness = None
        self.reproductionProbability = None

    def fitness(self):
        self.fitness = 1 / Genetic.calcF(self) //вызываю статический метод другого моего класса
        return self.fitness

    def reproductionProbability(self):
        self.reproductionProbability = self.fitness/Genetic.totalFitness
        return self.reproductionProbability

After in the code I wrote:
specie = Specie(10, 2, 4, 5)

print(specie.fitness())

And I get an error:
Traceback (most recent call last):
  File "/Users/nikita/Desktop/Python/geneticAlg/index.py", line 71, in <module>
    print(specie.fitness())
TypeError: 'NoneType' object is not callable

Perhaps I somehow initialize the class incorrectly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Grigoriev, 2020-09-21
@wb_by

For starters, python is not a compiled language.
Then you first created a method called fitness, and then in __init__ you redefined it to None
self.fitness = None
, it's all logical that you have an error.
Do not do it this way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question