| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> Python知识库 -> Python __new__method testifying and my other perspectives -> 正文阅读 |
|
[Python知识库]Python __new__method testifying and my other perspectives |
After an afternoon I've finally got a basic understanding of python's __new__method. Although still do I feel sorrow for the scanty knowledge because the basic implementation is still C, which I even haven't begun. 路漫漫其修远兮,吾将上下而求索。 The way ahead is long; I see no ending,yet high and low I'll search with my will unbending. The general idea which inspired me is a pseudo-code from the book named Fluent Python,?collected at page 615:
The pseudocode above is really manifest. The reason we call it pseudocode is because the source code is C, but it is achieved by python. Nonetheless, it doesn't matter for understanding what's going on: As we see, once an instance is created, __new__ method, the customed class's __new__method,?is the first one being processed. And the most interesting one is that the customed __init__ method is still processed before the new_object returning. Which means: In every object creation, both the? ---copied from Chris Freemanhttps://teamtreehouse.com/community/why-do-i-have-to-use-self-strnewargs-kwargs-i-tried-not-using-it-and-it-worked So, the real constructor method is __new__. The reason we often refer to __init__ as the constructor method is because we adopted jargon from other languages. __new__ umst return an instance, and that instance will in turn be passed as the first argument self,?then goes on constructing other instance methods by ourselves. The following code is what I used?to testify:
What a strange thing but not all in threotical. The __new__ method in the code above doesn't construct an object because it returns a list kwargs, which turns the TraditionalCar() into a dict class. Which means __init__ doesn't initialise the class and all kwargs becomes the key and values of an instance of dict data structure. But?when the Name in __new__ is not True:
__init__ method has been successfully processed and the object has been?successfully created.?But remember, it is construced by the __new__ method of the base object: object!!? If the customed class is inherited from another. It may become a bit different: class ReversedStr(str): def __new__(cls, *args, **kwargs): self = str.__new__(cls, *args, **kwargs) #cls included here too self = self[::-1] return self # Console: # test = ReversedStr("Hello") # >>>test # output: "olleH" It is because:?The advantage of using the? ---copied from Chris FreemanWhy do I have to use self = str.__new__(*args, **kwargs)? I tried not using it and it worked. (Example) | Treehouse Community Furthermore, if we go deeper for the class inheritance, which is really complicated but still significant for pointing out, is : if the code is like the underneath:
You are not allowed to add more arguments after cls (cls stands for class A) or an error will be raised:
This error can be found in CPython's source code, which is opened on github:
Because every class is automatically inherited from object, the basic object. Yes, the basic object is object, which is a basic knowledge. However, when I go deeper... We all know creating a class is actually using type(), the metaclass. And if you gather them within several sentences:
I won't explain that much. Maybe one day I will be?expertised in C.... And I will turn back finding out. |
|
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 | -2024/12/31 4:10:20- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |