Python 中的特殊双下划线方法,,python类内部部
Python 中的特殊双下划线方法,,python类内部部
python类内部部分双下划线特殊方法
__setattr__, __getattr__, __delattr__, hasattr __getattribute__ __getitem__(),__setitem__(), __delitem__()
主程序如下:class Foo:def _inif(self,pname): self.pname = pnamedef func(): print(‘i‘m func‘)def __getattr__(self, item): print(‘提示:属性[%s]不存在。‘%item)def __setattr__(selft, key, value): print(‘提示:属性赋值操作:[%s=%s]‘%(key,value)) self.__dict__[key]=value #self.key=value #无法使用,会无限递归,这就是调用本身__setattr__def __delattr__(self, item): print(‘提示:删除属性操作:删除[%s]‘%item) if item in self.__dict__: self.__dict__.pop(item) #del self.item #无限递归了 #无法使用,会无限递归,这就是调用本身__delattr__
1、得到属性值。__getattr__方法的运行流程,以及getattr的运行流程。当不存在属性名/方法名时,查找__getattr__()方法是否存在,存在即执行它。不存在再查看有没有默认参数,有则返回,没有则报错。
2、判断属性/方法名是否存在。__getattr__方法的运行流程,以及hasattr的运行流程
3、给属性赋值。setattr(对象名,属性名字符串,属性值),例如:setattr(f1,‘y‘,‘ccc‘)。
当__setattr__方法存在时,执行它内部的程序,必须执行:self.__dict__[key]=value,要不然不会进行赋值。(该方法默认返回值是None,一般不会使用)
4、删除属性。delattr(对象名,属性名字符串),例如:delattr(f1,‘y‘)。
当__delattr__方法存在时,执行它内部的程序,必须执行:
ifiteminself.__dict__:#要进行判断,如果键不存在,pop会出错。
self.__dict__.pop(item)
要不然不会进行删除。(该方法默认返回值是None,一般不会使用)
5.__getattribute__方法,只要调用属性就会进行__getattribute__方法,只有遇到raiseAttributeError()(必须是AttributeError异常)才会执行__getattr__
class Foo: def __init__(self, guestname, guestmobile): self.guestname = guestname self.guestmobile= guestmobile def __getattribute__(self, item): print(‘获取属性值操作 %s‘%item) if item != ‘guestname‘: raise AttributeError() def __getattr__(self, item): print(‘找不到属性 %s‘%item)f = Foo(‘小王‘,‘13945784807‘)f.guestnamef.guestage
执行结果:
获取属性值操作guestname
获取属性值操作guestage
找不到属性guestage
6.操作对象的属性/方法名时,像字典一样操作__getitem__(),__setitem__(),__delitem__(),具体操作字典方法要在其中自己写。以下代码只列了setitem,其余的都操作self.__dict[xx]=xx就可以。
class Foo:passf =Foo()f[‘a‘]=12执行结果:TypeError: ‘Foo‘ object does not support item assignment
class Foo: def __setitem__(self, key, value): print(‘run setitem‘) self.__dict__[key]=valuef =Foo()f[‘a‘]=12print(f.a)执行结果:run setitem12
口诀:
对象/类的点的操作,都和attr相关
对象/类的中括号的操作,都和item相关
7.__str__和__repr__,对象返回值的方法,他们内部必须以return返回,必须返回str类型的值。
a.当编译器提示性返回时,就调用__repr__()
b.当print或者str(对象实例名),就调用__str__()
>>> class Foo:... def __str__(self):... return ‘ggg‘... def __repr__(self):... return ‘xxx‘...>>> f = Foo()>>> fxxx>>> class Foo:... def __str__(self):... return ‘ooo‘...>>> f=Foo()>>> f<__main__.Foo object at 0x000001AB94DC9C18>>>> print(f)ooo>>>
口诀:print()方法变量方法顺序,找str,再找repr
‘‘‘str函数或者print函数--->obj.__str__()repr或者交互式解释器--->obj.__repr__()如果__str__没有被定义,那么就会使用__repr__来代替输出注意:这俩方法的返回值必须是字符串,否则抛出异常‘‘‘
Python 中的特殊双下划线方法
相关内容
- python基础之数据类型补充(基础五),,第七章 python
- pythonp爬虫 爬取百度音乐,,#coding=ut
- python基础知识 05 python语言中的大整数,,第五课 python
- Python3中的open函数,,open(file,
- Python 字典 len()方法,,描述Python 字
- Python(64)_写函数,判断用户传入的值(字符串,列表,
- python学习-10 运算符1,,1.加+,减-,乘*
- [原创]win7/64位系统+python3.7.2下安装wordcloud库失败之解决
- python 将IP地址转换成打包后的32位格式,,python 2.7
- python3.6---isin,,isin:相当于ex
评论关闭