📜  python 类方法 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:16.435000             🧑  作者: Mango

代码示例1
# classmethod example
In [20]: class MyClass:
    ...:     @classmethod
    ...:     def set_att(cls, value):
    ...:         cls.att = value
    ...:

In [21]: MyClass.set_att(1)

In [22]: MyClass.att
Out[22]: 1

In [23]: obj = MyClass()

In [24]: obj.att
Out[24]: 1

In [25]: obj.set_att(3)

In [26]: obj.att
Out[26]: 3

In [27]: MyClass.att
Out[27]: 3