@property @classmethod @staticmethod
@property
class test:
@property
def tt(self):
print('hehehe')
def jj(self):
print('gggg')
obj=test()
obj.tt
print(obj.jj)
实例化以后拿到对象obj
如果添加@property ,obj.tt可以直接拿到这个函数执行结果 heheh
然而不添加@property obj.jj的执行结果,要拿到结果需要添加括号来获取 obj.jj() 才可以拿到 gggg
所以为了代码的简洁方便,通过添加@property,只需要直接 obj.tt不需要加括号就可以获取函数执行结果
=============================================
staticmethod
@
@staticmethod
class test1:
def tt():
print("哈哈哈")
test1.tt() 如果不加装饰器会报错,说给了一个参数,因为默认会把self这个参数对应对象本身,如果你只是想简单把里面当一个函数使用,就加装饰器saticmethod
test1.tt() 加这个装饰器这样就不会报错了,结果为

浙公网安备 33010602011771号