当前位置:网站首页>Object类,以及__new__,__init__,__setattr__,__dict__
Object类,以及__new__,__init__,__setattr__,__dict__
2022-06-28 02:08:00 【xx_xjm】
总结:
1:__new__用于构建类的实例,而__init__用于初始化实例
2:__setattr__实际上就是往__dict__里面添加新的键值对
3:object类是python中所有类的父类,也就是说python中的类都是继承自object,所以当定义一个类,没有显示的指明它的父类的时候,实际上这个类也是有父类的,就是object。而__new__,__init__,__setattr__,__dict__这些其实都是object类的方法或者属性,我们自定义类的使用,如果用到这些,其实是对object的这些方法的重写。
4:补充:__str__, __repr__
一:__new__和__init__
参考自:
(12条消息) Python中的__new__和__init__的区别_耀凯考前突击大师的博客-CSDN博客_pythonnew和init的区别(12条消息) Python 类中的object是什么_NickTheRock的博客-CSDN博客_object在python中是什么意思(12条消息) Python中的__new__和__init__的区别_耀凯考前突击大师的博客-CSDN博客_pythonnew和init的区别
在实例化一个类别的时候,实际上是先调用__new__,再调用__init__的,而只有__new__返回一个实例,才会调用__init__,否则__init__方法不会被调用。
且__new__方法的第一个参数不是self,而是cls,因为self指代实例,而__new__函数调用的时候,实例还没有创建,所以不能是self, 而是__new__(cls),相应的,__new__方法返回的实例是
super().__new__(cls),也就是调用基类Object的__new__方法
二:__setattr__和__dict__
__dict__包含了一个类的所有属性键值对,但类和实例的__dict__还是有区别的:如下,实例的__dict__只包含自己定义的属性,二类的__dict__包含object的内置属性
参考自:(12条消息) Python中__dict__用法_7Drunk的博客-CSDN博客_python中__dict__

再来说__setattr__和__dict__的关系,对于__setattr__这个内置函数,当对类定义属性的时候,就会自动调用这个函数,如下图所示:


此时注意,self.__dict__[key] = value和object.__setattr__(self,key,value)是等价的,第一个相当于直接修改类的__dict__,而后者则是通过调用父类object的__setattr__来修改类的__dict__;关于__setattr__函数更详细的信息可以参考:
(12条消息) Python 魔法方法(三) __getattr__,__setattr__, __delattr___yusuiyu的博客-CSDN博客___getattr__()
补充:对于__getattr__,这个函数只有在调用类没有的属性的时候,类会自动调用到这个函数,但是它不会把这个新属性加到__dict__,也就是__getattr__不会调用__setattr__

同样,对于给类赋予新的属性,只会调用__setattr__,而不会调用__getattr__

三:__str__,__repr__
重点:
1:两个函数必须有返回值,且返回值必须是字符串,且没有__str__的情况下,会调用__repr__
2:参考自:
(12条消息) python中%s与%r的区别_fanzuifenzi_zsc的博客-CSDN博客_python中%r(12条消息) python--基础知识点--__str__、__repr___Chasing__Dreams的博客-CSDN博客
- __str__触发方式
- prtin()
- str()
- format()
- __repr__触发方式
- 在交互模式下直接使用引用对象的变量名查看对象。即在终端下输入变量名,再直接回车
- repr()
值得注意的是s%会将变量传入str()函数,而r%会将变量传入到repr
参考自:(12条消息) Python中__str__和__repr__的区别_Python 学习者的博客-CSDN博客
(12条消息) Python 类内置方法 __str__ , __repr___桃子小迷妹的博客-CSDN博客
(12条消息) python中%s与%r的区别_fanzuifenzi_zsc的博客-CSDN博客_python中%r
对于类来说,这两个内置函数是用来表达对象的,但我门定义一个类,如果不设置__str__或者__rper__函数,则返回的是这个对象的来源和内存地址:

如果此时设置了__str__,则会调用它

对于__repr__函数,如果没有定义__str__,则本来应该调用__str__的就会调用__repr__:

边栏推荐
- What if win11 cannot use dynamic wallpaper? Solution of win11 without dynamic wallpaper
- [kotlin] basic introduction and understanding of its syntax in Android official documents
- 嵌入式DSP音频开发
- 您的物联网安全性是否足够强大?
- Basic flask: template rendering + template filtering + control statement
- What is the best and safest software to download when buying stocks?
- QEMU monitor usage
- 【522. 最长特殊序列 II】
- 2022电工(初级)复训题库及在线模拟考试
- Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
猜你喜欢

分布式事务解决方案Seata-Golang浅析
![[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam](/img/54/df623fc1004e1dca5d369b4ed2608c.png)
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam

嵌入式软件开发中必备软件工具

Gateway微服务路由使微服务静态资源加载失败
![[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics](/img/91/d7d6137b95f6348f71692164614340.png)
[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics

Arduino esp8266 web LED control

Redis搭建集群【简单】

Get 5 offers after being notified of layoffs
![[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code](/img/ef/a26127284fe57ac049a4313d89cf97.png)
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code

Brief history and future trend of codeless software
随机推荐
[today in history] June 16: Oracle Bone Inscriptions was established; Microsoft MSX was born; The inventor of fast Fourier transform was born
2-5 basic configuration -win2003 add attack surface
Heartless sword English Chinese bilingual poem 004 Meditation
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
Is it reliable to invest in the inter-bank certificate of deposit fund? Is the inter-bank certificate of deposit fund safe
Domain Name System
What are the good practices of cloud cost optimization?
抓包整理外篇fiddler————了解工具栏[一]
[issue 21] face to face experience of golang engineer recruited by Zhihu Society
CMU提出NLP新范式—重构预训练,高考英语交出134高分
【活动早知道】LiveVideoStack近期活动一览
网上股票投资交流群安全吗?进群免费开户靠谱嘛?
无心剑英汉双语诗004.《静心》
PPT制作小技巧
腾讯游戏发布40多款产品与项目 其中12款为新游戏
s32ds跳转到DefaultISR
一位博士在华为的22年(干货满满)
Tencent games released more than 40 products and projects, including 12 new games
3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力
[522. longest special sequence II]