Please provide support with this Class method example

Question:

class Person:
    number_of_people = 0

    def __init__(self,name):
        self.name = name
        Person.add_person()

    @classmethod                        
    def number_of_people_(cls):         
        return cls.number_of_people()

    @classmethod
    def add_person(cls):
        cls.number_of_people += 1

p1 = Person('Tim')
p2 = Person('Jill')
print(Person.number_of_people_())

The code above gives

TypeError: 'int' object is not callable

Please help with this!

Asked By: VivorKeleve

||

Answers:

Your TypeError is because of what you are trying to return in your classmethod:

umber_of_people = 0

def number_of_people_(cls):         
    return cls.number_of_people() # you are calling the attribute above which is set to 0

Change to:

def number_of_people_(cls):         
    return cls.number_of_people
Answered By: AshSmith88
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.