当前位置:网站首页>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__:
边栏推荐
- 空闲中断无法清除
- R语言惩罚逻辑回归、线性判别分析LDA、广义加性模型GAM、多元自适应回归样条MARS、KNN、二次判别分析QDA、决策树、随机森林、支持向量机SVM分类优质劣质葡萄酒十折交叉验证和ROC可视化
- 【iptables&icmp】iptables默认策略中关于icmp协议的说明
- Heartless sword Chinese English bilingual poem 004 Sword
- 基于流的深度生成模型
- 抓包整理外篇fiddler————了解工具栏[一]
- 建立自己的网站(17)
- JS clear the object and its value:
- crond BAD FILE MODE /etc/cron.d
- 云成本优化有哪些优秀实践?
猜你喜欢
2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
如何获取GC(垃圾回收器)的STW(暂停)时间?
多快好省,低门槛AI部署工具FastDeploy测试版来了!
业内首个!可运行在移动设备端的视频画质主观体验MOS分评估模型!
根据Explain查看sql执行计划,对SQL进行优化
Dataloader参数collate_fn的使用
Single page application (SPA) hash route and historical API route
2022安全员-C证考试题库模拟考试平台操作
2022电工(初级)复训题库及在线模拟考试
Excel知识技能汇总
随机推荐
新手开哪家的证券账户是比较好?炒股开户安全吗
Brief history and future trend of codeless software
音视频技术开发周刊 | 251
拾光者,云南白药!
音视频技术开发周刊 | 251
How to write concise code? (upper)
Notepad++--列编辑模式--用法/实例
Use js programming questions [split] in Niuke
Idea auto generate code
Etcd database source code analysis -- network layer server rafthandler between clusters
Reading makes people quiet
买股票应该下载什么软件最好最安全?
JS clear the object and its value:
为什么OpenCV计算的帧率是错误的?
Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
ETCD数据库源码分析——集群间网络层服务端RaftHandler
【活动早知道】LiveVideoStack近期活动一览
国泰君安证券靠谱吗?开证券账户安全吗?
In the digital era, enterprises must do well in user information security
将PCAP转换为Json文件的神器:joy(安装篇)