python 内置函数staticmethod
python staticmethod()返回函数的静态方法----该方法不强制要求传递参数
@staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样
class a(object):
@staticmethod
def fun():
print('hello world')
a_object = a()
a_object.fun()
a.fun()
# hello world
# hello world
python 内置函数classmethod
classmethod修饰符对应的函数不需要实例化,不需要self参数,但第一个参数需要是表示自身类的cls参数,可以用来调用类的属性,类的方法,实例化对象等
class A(object):
bar = 1
def func(self):
print('foo')
@classmethod
def fun2(cls):
print('func2')
print(cls.bar)
cls().func()
A.fun2()
# 打印结果为:
func2
1
foo
python 内置函数property
装饰器 即:在方法上应用装饰器
class Goods:
"""python3中默认继承object类
以python2、3执行此程序的结果不同,因为只有在python3中才有@xxx.setter @xxx.deleter
"""
@property
def price(self):
print('@property')
@price.setter
def price(self, value):
print('@price.setter')
@price.deleter
def price(self):
print('@price.deleter')
# ############### 调用 ###############
obj = Goods()
obj.price # 自动执行 @property 修饰的 price 方法,并获取方法的返回值
obj.price = 123 # 自动执行 @price.setter 修饰的 price 方法,并将 123 赋值给方法的参数
del obj.price # 自动执行 @price.deleter 修饰的 price 方法
打印结果为:
@property
@price.setter
@price.deleter