python - 内置对象 之 内置装饰器 @staticmethod @classmethod @property

目录

1.@property

2.@staticmethod 静态方法

3.@classmethod 类方法


 

Python内置有三大装饰器:@staticmethod(静态方法)、@classmethod(类方法)、@property(描述符),其中静态方法就是定义在类里的函数,并没有非要定义的必要;类方法则是在调用类属性、传递类对象时使用。

 

1.@property

@property有两大功能:

  • 定义只读属性
  • 限制属性的定义

(1)定义只读属性

class Person(object):
    def __init__(self, name, age=18):
        self.name = name
        self.__age = 18

    @property
    def age(self):
        return self.__age
        
xm = Person('xiaoming')  #定义一个人名小明
print(xm.age)	#结果为18
xm.age = -4	#报错无法给年龄赋值
print(xm.age)

结果:
18
Traceback (most recent call last):
  File "/Users/juvo/ProjectScript/TestScript/test02.py", line 13, in <module>
    xm.age = -4  # 报错无法给年龄赋值
AttributeError: can't set attribute
  • 注意:加了@property后,可以用调用属性的形式来调用方法,后面不需要加()
  • 另外,property 有 getter,setter 和 deleter 三种方法可以用作装饰器
  • 1》只有@property表示 只读。
    2》同时有@property和@x.setter表示 可读可写。

    3》同时有@property和@x.setter和@x.deleter表示可读可写可删除。

 

(2)限制属性的定义

  • 如下实现Person类,规定每个人(即创建的实例)的年龄必须大于18岁
class Person(object):
    def __init__(self, name):
        self.name = name
        self.__age = 18

    @property
    def age(self):
        return self.__age

    def set_age(self, age):  # 定义函数来给self.__age赋值
        if age < 18:
            print('年龄必须大于18岁')
            return
        self.__age = age
        return self.__age


xm = Person('xiaoming')
print(xm.age)
print('----------')
xm.set_age(10)
print(xm.age)
print('----------')
xm.set_age(20)
print(xm.age)

18
----------
年龄必须大于18岁
18
----------
20


#上述代码简化为
class Person(object):
    def __init__(self, name):
        self.name = name
        self.__age = 18

    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self, age):  # 定义函数来给self.__age赋值
        if age < 18:
            print('年龄必须大于18岁')
            return
        self.__age = age
        return self.__age


xm = Person('xiaoming')
print(xm.age)
print('----------')
xm.age = 10
print(xm.age)
print('----------')
xm.age = 20
print(xm.age)

 

2.@staticmethod 静态方法

python staticmethod 返回函数的静态方法。

class C:
    name = 'len'
    gender='male'

    def __init__(self,a,w):
        self.age=a
        self.weight=w

    def func1(self):
        print(self.name)  # 调用当前类的属性,通过self.属性调用

    # 静态方法装饰器
    @staticmethod
    def func2():
        print(C.name)     # 调用当前类的属性,通过类名.属性调用


if __name__=='__main__':
    #不实例人类调用
    C.func2()
    #实例调用
    len=C(30,140)
    len.func2() 

 

3.@classmethod 类方法

classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。

class A(object):
    bar = 1
    def func1(self):  
        print ('foo') 
    @classmethod
    def func2(cls):
        print ('func2')
        print (cls.bar)
        cls().func1()   # 调用 foo 方法
 
A.func2()               # 不需要实例化
class printnum():
    method = '@classmethod往构造函数返回参数'

    def __init__(self, num1, num2, num3):
        self.num1 = num1
        self.num2 = num2
        self.num3 = num3

    def print_num(self, num4):
        print(self.num1, self.num2, self.num3, num4)

    @classmethod
    def list_to_num(cls, threenum):
        print(cls.method)
        n1 = threenum[0] + 1
        n2 = threenum[1] + 2
        n3 = threenum[2] + 3
        return cls(n1, n2, n3)
        # 将n1,n2,n3分别返回到num1,num2,num3
        # return cls(num1=n1,num2=n2,num3=n3)

if __name__ == '__main__':
    list = [4, 5, 6]
    b = printnum.list_to_num(list)
    b.print_num(11)   

#结果:
#@classmethod往构造函数返回参数
#5 7 9 11

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值