Mehdi Mostafavi
Mehdi Mostafavi

Reputation: 880

missing 1 required positional argument: 'self' while using classmethod in staticmethod

I write class Person in Python 3.8. When I use calc_all() method to calculate income for every instance in instances, I get an error. Code:

import math
class Person:
    instances = []
    @classmethod
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.level = 1
        self.job = ""
        self.work_place = None
        Person.instances.append(self)
    def do_level(self,income):
        return income*math.sqrt(self.level*self.work_place.level)
    def calc_income(self):
        pass
    def calc_life_cost(self):
        pass
    def calc(self):
        income = self.calc_income()
        cost = self.calc_life_cost()
        return self.do_level(income) - cost
    @staticmethod
    def calc_all():
        zigma = 0
        for instance in Person.instances:
            zigma = zigma + instance.calc()
        return zigma

p = Person('X',12)
p2 = Person('Y',15)
print(Person.calc_all())

Error:

zigma = zigma + instance.calc()
TypeError: calc() missing 1 required positional argument: 'self'

Upvotes: 0

Views: 683

Answers (1)

Anvar Kurmukov
Anvar Kurmukov

Reputation: 662

What are you trying to accomplish by making __init__ a classmethod? You could freely remove it. After this you will still have an error, since self.work_place does not have attribute level.

Upvotes: 2

Related Questions