python之property、类方法和静态方法

1、完整的property
一、定义
一个方法被假装成属性以后,应该能够执行一个属性的增删改查操做,
增长和修改就对应着被setter装饰的方法,
删除一个属性对应着被deleter装饰的方法。post

@property:把方法假装成属性,只能有一个参数selfui

@被property装饰的方法名.setter:
当被property装饰的方法,又实现了一个同名方法,且被setter装饰器装饰了,
那么在对被装饰的方法赋值的时候,就会触发被setter装饰器装饰的方法,
这个方法必需要传一个参数接收等号后面的值,
是用来保护一个变量在修改的时候可以添加一些保护条件。this

@被property装饰的方法名.deleter:
当被property装饰的方法,又实现了一个同名方法,且被deleter装饰器装饰了,
那么在对被装饰的方法进行删除的操做时,就会触发被deleter装饰器装饰的方法,
这个方法并不能在执行的时候真的删除这个属性,而是你在代码中执行什么就有什么效果.spa


二、例题code

复制代码
学生类
class Student:
    def __init__(self,name):
        self.__name = name

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self,new):
        if type(new) is str:   #由于名字是字符串类型的,咱们这样设置能够保证只能用字符串修更名字
            self.__name = new

    @name.deleter
    def name(self):
        del self.__name

xiaoming = Student('小明')
print(xiaoming.name)   #小明

xiaoming.name = 123   # 不是字符串修改不了
print(xiaoming.name)   # 小明

xiaoming.name = '小花猫'
print(xiaoming.name)   # 小花猫

del xiaoming.name
print(xiaoming.__dict__) # {} 空字典


水果类:
class Fruits:
    __discount = 0.7

    def __init__(self,price):
        self.__price = price

    @property
    def price(self):
        return self.__price * Fruits.__discount

    @price.setter
    def price(self,new):
        if type(new) is int or float:
            self.__price = new

    @price.deleter
    def price(self):
        del self.__price

banana = Fruits(10)
print(banana.price)  # 折扣价7.0


banana.price = 9
print(banana.price) # 折扣价6.3

del banana.price
print(banana.__dict__)  # {} 空字典
复制代码

 


三、总结:
被setter和deleter装饰的方法名必须和被property装饰的方法名一致,对象.方法名 不加括号 能够调用被property装饰的方法,
当对被property装饰的方法赋值时,就会触发被setter装饰的方法,当对被property装饰的方法进行删除del操做时,就会触发
被deleter装饰的方法。
注意:(通常来讲用的最多的是property,其余两个看状况而使用)对象

 

 

2、类方法:
用@classmethod装饰
经过类名调用,
类方法默认形参用cls表示,而不用self,
即便用对象去调用类方法,cls默认传进去的仍是类的命名空间地址,
能够直接经过类去修改类的属性,不须要实例化。blog

复制代码
class Fruits:
    __discount = 0.7  # 类的静态属性

    def __init__(self,price):
        self.__price = price  # 对象的私有属性

    @property
    def price(self):
        return self.__price * Fruits.__discount

    @classmethod
    def change_discount(cls,new):  # 类方法默认形参用cls表示,而不用self
        cls.__discount = new


Fruits.change_discount(0.6)
print(Fruits.__dict__)  # '_Fruits__discount': 0.6
复制代码

类方法的特色:
只使用类中的资源,且这个资源能够直接用类名引用,那这个方法应该被改成一个类方法继承

 


3、静态方法
被@staticmethod装饰的方法,不使用类中的命名空间也不使用对象的命名空间,
能够传参,也能够不传参,没有默认参数(self,cls),至关于一个类外的普通的方法,
不一样的是调用的时候须要 类名.方法名资源

复制代码
class Student:
    @staticmethod
    def login():
        print('登陆成功')

Student.login()
复制代码

 

 

4、类中的方法属性总结
类:  成员:       标准使用者:       默认形参:
    静态属性      类/对象                     
    类方法       类         cls 表示类
    静态方法      类
    方法        对象        self 表示对象
    property方法    对象        self 表示对象字符串

 注意:有些成员用类和对象都是能够调用的,不过建议按照标准使用者去调用。

5、
一、isinstance:判断一个对象是不是一个已知的类型
print(type(123) is int)           # True
print(isinstance(123,int))     # True


# isinstance还能够检测对象与类之间的关系(包括继承)
# type不能检测继承关系

 

复制代码
class A:
    pass
class B(A):
    pass
a = A()
b = B()


print(type(a) is A) # True
print(type(b) is B) # True
print(type(b) is A) # False

print(isinstance(a,A)) # True
print(isinstance(b,B)) # True

# 子类是父类的类型,可是父类不是子类的类型
print(isinstance(b,A)) # True
print(isinstance(a,B)) # False
复制代码

 

 

 

二、issubclass:检测类与类之间的关系
用法:issubclass(子类,父类)

复制代码
class A:
    pass
class B(A):
    pass
print(issubclass(A,B))  # False
print(issubclass(B,A))  # True
复制代码
相关文章
相关标签/搜索