列举Python面向对象中带双下划线的特殊方法
__new__:生成实例
__init__:生成实例的属性
__call__:实例对象加()会执行def__call__:...方法里边的内容。
__del__:析构方法,当对象在内存中被释放时,自动触发执行。如当delobj或者应用程序运行完毕时,执行该方法里边的内容。
__enter__和__exit__:出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量;with中代码块执行完毕时执行__exit__里边的内容。
__module__:表示当前操作的对象在那个模块obj.__module__
__class__:表示当前操作的对象的类是什么obj.__class__
__doc__:类的描述信息,该描述信息无法被继承
__str__:改变对象的字符串显示print函数--->obj.__str__()
__repr__:改变对象的字符串显示交互式解释器--->obj.__repr__()
__format__:自定制格式化字符串
__slots__:一个类变量用来限制实例可以添加的属性的数量和类型
__setitem__,__getitem,__delitem__:
classFoo:
def__init__(self,name):
self.name=name
def__getitem__(self,item):
print(self.__dict__[item])
def__setitem__(self,key,value):
self.__dict__[key]=value
def__delitem__(self,key):
print('delobj[key]时,我执行')
self.__dict__.pop(key)
def__delattr__(self,item):
print('delobj.key时,我执行')
self.__dict__.pop(item)
f1=Foo('sb')
f1['age']=18
f1['age1']=19
delf1.age1
delf1['age']
f1['name']='alex'
print(f1.__dict__)
__get__():调用一个属性时,触发
__set__():为一个属性赋值时,触发
__delete__():采用del删除属性时,触发
__setattr__,__delattr__,__getattr__:
以上内容为大家列举了Python面向对象中带双下划线的特殊方法,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中的filter函数功能是什么?
python中的filter函数功能是什么?在python中,面对众多的数据,我们要过滤筛选出我们需要的数据。python中的filter函数就是起到了过滤筛选的作...详情>>
2023-11-10 20:37:27
pythontime模块是什么
pythontime模块是什么在python中使用时间,就免不了和time模块打交道,另外两个模块这个暂时先不做介绍。做time模块的使用上,我们可以用它来对...详情>>
2023-11-10 15:53:16
python是什么编程语言
python是什么编程语言1、说明是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Pyt...详情>>
2023-11-10 15:21:05
python异常处理的两种技巧
python异常处理的两种技巧1、传递异常有时我们会在捕捉到一个异常后重新引发它(传递异常),实现起来很简单,使用不带参数的raise语句即可。deff...详情>>
2023-11-10 14:49:39热门推荐
python中的filter函数功能是什么?
沸python delattr函数如何使用?
热python中pdb模块怎么用?
热Python如何截图保存?
新python中缺少module怎么办?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python对象的三要素是什么
pythonGIL在Python多线程的应用
python如何对多个CSV文件进行读取
pythonif嵌套命令如何理解?
python对列表进行永久性或临时排序的方法
python生成器调用方法引发异常
技术干货






