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

时间:2025-01-18 08:37:56

一、完整的property

1、定义
一个方法被伪装成属性之后,应该可以执行一个属性的增删改查操作,
增加和修改就对应着被setter装饰的方法,
删除一个属性对应着被deleter装饰的方法。

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

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

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

2、例题

学生类
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__) # {} 空字典

3、总结:
被setter和deleter装饰的方法名必须和被property装饰的方法名一致,对象.方法名 不加括号 可以调用被property装饰的方法,
当对被property装饰的方法赋值时,就会触发被setter装饰的方法,当对被property装饰的方法进行删除del操作时,就会触发
被deleter装饰的方法。
注意:(一般来说用的最多的是property,其他两个看情况而使用)

二、类方法

用@classmethod装饰
通过类名调用,
类方法默认形参用cls表示,而不用self,
即使用对象去调用类方法,cls默认传进去的还是类的命名空间地址,
可以直接通过类去修改类的属性,不需要实例化。

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

类方法的特点:
只使用类中的资源,且这个资源可以直接用类名引用,那这个方法应该被改为一个类方法

三、静态方法

被@staticmethod装饰的方法,不使用类中的命名空间也不使用对象的命名空间,
可以传参,也可以不传参,没有默认参数(self,cls),相当于一个类外的普通的方法,
调用的时候可以:  类名.方法名 或者 对象.方法名

class Student:
@staticmethod
def login():
print('登录成功') Student.login()

四、类中的方法属性总结

类:  成员:       标准使用者:       默认形参:
    静态属性      类/对象                     
    类方法       类         cls 表示类
    静态方法      类
    方法        对象        self 表示对象
    property方法    对象        self 表示对象

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

五、几个判断方法

1、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

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

class A:
pass
class B(A):
pass
print(issubclass(A,B)) # False
print(issubclass(B,A)) # True