How can I access a classmethod from inside a class in Python

前端 未结 6 1016
日久生厌
日久生厌 2020-12-03 06:40

I would like to create a class in Python that manages above all static members. These members should be initiliazed during definition of the class already. Due to the fact t

6条回答
  •  一整个雨季
    2020-12-03 07:19

    person class with add() @classmethod

    class Person:
        def __init__(self,a,b):
                self.first = a
                self.second = b
    
        @classmethod
        def add(cls,a,b):
            return a+b
        
        def __repr__(self):
            return ('{}'.format(Person.add(self.first,self.second)))
        
    p = Person(10,20)
    print(p)
    
    o/p : 30
    

提交回复
热议问题