Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Please provide support with this Class method example

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading