当前位置:网站首页>Object class, and__ new__,__ init__,__ setattr__,__ dict__
Object class, and__ new__,__ init__,__ setattr__,__ dict__
2022-06-28 03:25:00 【xx_ xjm】
summary :
1:__new__ For building instances of classes , and __init__ Used to initialize the instance
2:__setattr__ Actually, it's going to __dict__ Add a new key value pair
3:object Class is python The parent of all classes in , in other words python All classes in are inherited from object, So when defining a class , When there is no indication of its parent class , In fact, this class also has a parent class , Namely object. and __new__,__init__,__setattr__,__dict__ These are all object Class methods or properties , We customize the use of classes , If you use these , Is to object Rewrite of these methods .
4: Add :__str__, __repr__
One :__new__ and __init__
Reference from :
(12 Bar message ) Python Medium __new__ and __init__ The difference between _ Yao Kai's blog of the former shock master -CSDN Blog _pythonnew and init The difference between (12 Bar message ) Python Class object What is it? _NickTheRock The blog of -CSDN Blog _object stay python What do you mean in English (12 Bar message ) Python Medium __new__ and __init__ The difference between _ Yao Kai's blog of the former shock master -CSDN Blog _pythonnew and init The difference between
When instantiating a class , It's actually calling __new__, Call again __init__ Of , And only __new__ Return an instance , Will call __init__, otherwise __init__ Method will not be called .
And __new__ The first parameter of the method is not self, It is cls, because self Reference instance , and __new__ When the function is called , Instance has not been created , So it can't be self, It is __new__(cls), Corresponding ,__new__ The instance returned by the method is
super().__new__(cls), That is to call the base class Object Of __new__ Method
Two :__setattr__ and __dict__
__dict__ Contains all attribute key value pairs of a class , But classes and instances __dict__ There's a difference : as follows , Example of __dict__ Include only the attributes you define , Second class __dict__ contain object Built-in properties
Reference from :(12 Bar message ) Python in __dict__ usage _7Drunk The blog of -CSDN Blog _python in __dict__

Again, __setattr__ and __dict__ The relationship between , about __setattr__ This built-in function , When defining attributes on a class , This function will be called automatically , As shown in the figure below :


Note that at this time ,self.__dict__[key] = value and object.__setattr__(self,key,value) It is equivalent. , The first is equivalent to modifying the class directly __dict__, The latter is done by calling the parent class object Of __setattr__ To modify the class __dict__; About __setattr__ For more detailed information about the function, please refer to :
Add : about __getattr__, This function is only used when calling properties that the class does not have , Class will automatically call this function , But it doesn't add this new attribute to __dict__, That is to say __getattr__ Not invoke __setattr__

Again , For assigning new attributes to classes , It will only call __setattr__, Instead of calling __getattr__

3、 ... and :__str__,__repr__
a key :
1: Both functions must have a return value , And the return value must be a string , And there's no __str__ Under the circumstances , Would call __repr__
2: Reference from :
(12 Bar message ) python in %s And %r The difference between _fanzuifenzi_zsc The blog of -CSDN Blog _python in %r(12 Bar message ) python-- Basic knowledge points --__str__、__repr___Chasing__Dreams The blog of -CSDN Blog
- __str__ Trigger mode
- prtin()
- str()
- format()
- __repr__ Trigger mode
- In interactive mode, directly use the variable name of the reference object to view the object . That is, input the variable name under the terminal , Then go straight to enter
- repr()
It is worth noting that s% Will pass variables into str() function , and r% Will pass variables into repr
Reference from :(12 Bar message ) Python in __str__ and __repr__ The difference between _Python Learners' blogs -CSDN Blog
For a class , These two built-in functions are used to express objects , But we define a class , If not set __str__ perhaps __rper__ function , The source and memory address of the object are returned :

If you set __str__, Call it

about __repr__ function , If there is no definition __str__, Should have called __str__ If you do, you will call __repr__:

边栏推荐
- Thesis reading: General advantageous transformers
- [games] Parkour
- JS clear the object and its value:
- s32ds跳转到DefaultISR
- __getitem__和__setitem__
- matlab习题 —— 矩阵的常规运算
- Arm development studio build compilation error
- Built in functions for MySQL database operations
- R语言惩罚逻辑回归、线性判别分析LDA、广义加性模型GAM、多元自适应回归样条MARS、KNN、二次判别分析QDA、决策树、随机森林、支持向量机SVM分类优质劣质葡萄酒十折交叉验证和ROC可视化
- service实现类里面为何一直报红
猜你喜欢

视频编解码性能优化与实现

2022 electrician (elementary) recurrent training question bank and online simulation examination

What are the technologies to be mastered in the test? Database design for software testing

ETCD数据库源码分析——集群间网络层服务端RaftHandler

s32ds跳转到DefaultISR

A16z:元宇宙解锁游戏基础设施中的新机遇

剑指 Offer 47. 礼物的最大价值(DP)

音视频技术开发周刊 | 251

Tips for visiting the website: you are not authorized to view the recovery method of this page

matlab习题 —— 矩阵的常规运算
随机推荐
Notepad++--常用的插件
导致系统性能失败的十个原因
Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)
栈的基本操作(C语言实现)
Win 10出现bitlocke恢复,蓝屏错误代码0x1600007e
Necessary software tools in embedded software development
2022 safety officer-c certificate examination question bank simulated examination platform operation
建立自己的网站(17)
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
启牛开的证券账户是安全的吗?如何开账户呢
【iptables&icmp】iptables默认策略中关于icmp协议的说明
2022电工(初级)复训题库及在线模拟考试
论文阅读:Generative Adversarial Transformers
Use js programming questions [split] in Niuke
剑指 Offer 49. 丑数(三指针法)
apache、iis6、ii7独立ip主机屏蔽拦截蜘蛛抓取(适用vps云主机服务器)
2022安全员-C证考试题库模拟考试平台操作
Build your own website (17)
Single page application (SPA) hash route and historical API route
Etcd database source code analysis -- network layer server rafthandler between clusters