[英]object and __init__ method
I am learning Python and I heard all functions are objects.我正在学习 Python,听说所有函数都是对象。 And classes are objects too, if I am not mistaken.
如果我没记错的话,类也是对象。 Then methods within a class must be objects.
那么 class 中的方法必须是对象。
Then I wonder, within __init__
method, which is an object, are the attributes objects, too?然后我想知道,在
__init__
方法中,这是一个 object,也是属性对象吗? Objects within an object? object 中的对象?
There is a phrase 'Everything is an object'.有一句话“一切都是对象”。 And my impression is that anything that holds a memory space is an object.
我的印象是任何拥有 memory 空间的东西都是 object。 What I don't quite grasp is to what extent the phrase 'everything is an object' applies.
我不太了解的是“一切都是对象”这句话的适用程度。 I wonder if it applies even to variables within
__init__
method.我想知道它是否甚至适用于
__init__
方法中的变量。
Yes, it's quite similar to Java.是的,它与 Java 非常相似。 Every Python variable hides a pointer to the right location of memory which contains the actual data.
每个 Python 变量都隐藏了一个指向 memory 正确位置的指针,其中包含实际数据。 There are only four primitive types in Python: integers, floats, booleans and strings;
Python中只有四种基本类型:整数、浮点数、布尔值和字符串; anything else is an ADT.
其他任何东西都是 ADT。 The keyword self is the same of this in Java or C++, in other words the self-reference.
在 Java 或 C++ 中,关键字self与此相同,即自引用。 Using self , you can invoke another method inside the class or access to an attribute.
使用self ,您可以调用 class 中的另一个方法或访问属性。 Syntax like
self.var = 1
can also creates a new attribute (if it doesn't exist yet) with name "var", type "int" and value 1.像
self.var = 1
这样的语法也可以创建一个名为“var”的新属性(如果它还不存在),类型为“int”,值为 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.