#与属性相关的魔术方法
classMan:#属性
sex = 'man'age= 18color='yellow'name= 'shizhipeng'
#方法
def __init__(self):
self.gf= 'cortana'self.hobby= 'watchTV'
defchou(self):print('piapia')defkeng(self):print('坑娃的')'''#__getattr__()
触发时机:访问一个不存在的成员属性的时候触发
功能:1.防止访问不存在成员报错 2.可以为不存在的成员设置默认值
参数:一个self接受对象 一个参数接受要访问的不存在的成员名
返回值:可以有可以没有,如果有则是设置了访问的成员的值
def __getattr__(self,attrname):
#print(attrname)
if attrname == 'height':
return '175cm'
elif attrname =='weight':
return '75kg'
else:
return '对不起,不存在该属性''''
'''#__getattribute__()
触发时机:访问对象成员时触发(无论是否存在)
功能:可以限制过滤指定成员的值的访问
参数:一个self接受当前对象 一个接受要访问的成员名
返回值:可以有,可以没有 推荐根据程序设定(一般有!)
def __getattribute__(self,attrname):
if attrname == 'name':
#不能通过当前对象访问成员(访问时绝对不允许使用[对象,成员名]的格式获取)触发递归!
return object.__getattribute__(self,attrname)
elif attrname == 'sex':
return object.__getattribute__(self,attrname)
else:
return'对不起,成员不存在或者不允许访问''''
#__setattr__
'''触发时机:对成员属性进行设置的时候触发
功能:限制或者过滤对象成员的修改
参数:一个self接受当前对象 一个接受修改的成员名 一个要修改的成员值
返回值:没有返回值!'''
def __setattr__(self,attrname,value):#可以在设置成员属性时候进行判断
if attrname == 'sex' or attrname == 'name':pass
else:
object.__setattr__(self,attrname,value)#__delattr__ 魔术方法
def __delattr__(self,attrname):#根据删除的内容决定是否允许删除
if attrname == 'hobby':pass
else:
object.__delattr__(self,attrname)#实例化对象
szp =Man()'''#获取对象的成员属性(getattr魔术方法)
print(szp.height)
print(szp.weight)
print(szp.length)'''
#获取对象的成员属性(getattribute)#print(szp.color)
#设置成员属性#szp.sex = 'no man no woman'#print(szp.sex)
#szp.name = '失了志'#print(szp.name)
#szp.color = 'black'#print(szp.color)
#删除成员属性
print(szp.__dict__)delszp.gfprint(szp.__dict__)#dir魔术方法
classHuman:#属性
sex = 'man'age= 18name= 'dog'
#方法
defeat(self):print('吃饭')defdrink(self):print('喝水')#__dir__
def __dir__(self):#获取所有可以访问的成员
lists = object.__dir__(self)#过滤魔术方法 只留下自定义成员
newlists =[]for i inlists:if i.startswith('__') and i.endswith('__'):pass
else:
newlists.append(i)returnnewlists#实例化对象
qw =Human()#获取当前对象中所有存在的成员
result =dir(qw)print(result)#其他魔术方法
#自定义我们自己的整型类
classMyint(int):'''#__add__加法的魔术方法(int类中本来就有)
def __add__(self,other):
#print('add方法被触发')
#print(self,other)
return int(self) - int(other)'''
#__radd__反向加法的魔术方法(int类中本来就有)
def __radd__(self, other):#print('radd方法被触发')
return int(self) *int(other)#实例化对象
no1 = Myint(50)#result = no1 + 15#print(result)
'''#练习:自定义一个整型,相加时进行的运算为10位数相乘+个位数相减
class Myint(int):
def __add__(self,other):
return int((self // 10) * (other // 10)) + int(self % 10 - other % 10)
no1 = Myint(18)
result = no1 + 25
print(result)'''
#反向加法运算__radd__
result = no1 + 40
print(result)'''#练习:自定义一个整型,反向相加运算时获取2个数值的个位数的乘积作为结果
class Myint(int):
def __radd__(self, other):
return int(self % 10) * int(other % 10)
no1 = Myint(18)
result = 25 + no1
print(result)'''
#比较运算
classMyint(int):#__eq__ 相等魔术方法
def __eq__(self,other):#print('eq方法被触发')
#print(self,other)
#自定义规则:如果2个数都能被6整除 相等 其余都不相等
if self % 6 == 0 and other % 6 ==0:returnTrueelse:returnFalse#__gt__ 大于比较魔术方法
def __gt__(self,other):if self % 5 > other % 5:returnTrueelse:returnFalse#实例化对象
no1 = Myint(34)
no2= Myint(63)#相等运算
result = no1 ==no2print(result)'''#练习:只要个位数相等 就相等 其余都不相等
class Myint(int):
def __eq__(self,other):
if (self % 10) == (other % 10):
return True
else:
return False
no1 = Myint(12)
no2 = Myint(22)
result = no1 == no2
print(result)'''
#大于比较运算
result = no1 >no2print(result)