I was just looking at one question here and the OP was using a same name for class, other things and also for variable. When I was trying to answer it, I became confused myself and thus thought of asking.
我只是在这里看一个问题而OP正在为类,其他东西以及变量使用相同的名称。当我试图回答它时,我自己变得困惑,因此想到了问。
For example:
例如:
class MyClass:
pass
MyClass=MyClass()
Though, I will never code anything like this. I would like to understand how this will be treated by python interpreter. So my question is, is the variable MyClass I will use will be created first or the other way? Which is, creating an instance of MyClass firstly and assigning it to MyClass variable. I think the latter is correct but if that is the case, how will the following be resolved?
虽然,我永远不会编码这样的东西。我想了解python解释器将如何处理它。所以我的问题是,我将使用的变量MyClass将首先创建还是以其他方式创建?也就是说,首先创建一个MyClass实例并将其分配给MyClass变量。我认为后者是正确的,但如果是这样的话,将如何解决以下问题?
class MyClass:
pass
MyClass=MyClass()
new_class=MyClass()
4 个解决方案
#1
3
class MyClass:
pass
MyClass=MyClass()
In simple terms, the above code does three things (in this order):
简单来说,上面的代码做了三件事(按此顺序):
-
Defines the class
MyClass
.定义类MyClass。
-
Creates an instance of
MyClass
.创建MyClass的实例。
-
Assigns that instance to the variable
MyClass
.将该实例分配给变量MyClass。
After the last step, the class MyClass
is overwritten and can no longer be used. All you have left is an instance of it contained in the variable MyClass
.
在最后一步之后,类MyClass被覆盖,无法再使用。你剩下的就是它包含在变量MyClass中的一个实例。
Moreover, if you try to call this instance as you would a class, you will get an error:
此外,如果您尝试像调用类一样调用此实例,则会出现错误:
>>> class MyClass:
... pass
...
>>> MyClass=MyClass()
>>> new_class=MyClass()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'MyClass' object is not callable
>>>
#2
4
The right-hand side of the assignment is processed first, so an instance of MyClass
is created. But then you reassign the name MyClass
to that instance. When you execute
首先处理赋值的右侧,因此创建了MyClass的实例。但是,您将名称MyClass重新分配给该实例。当你执行
new_class = MyClass()
you should get an error about MyClass
not being callable, since that name now refers to an instance of the original class, not the class itself.
你应该得到一个关于MyClass不可调用的错误,因为该名称现在指的是原始类的实例,而不是类本身。
#3
2
The line:
这条线:
new_class=MyClass()
in most cases will return an error, saying something like instance not callable.
在大多数情况下会返回一个错误,说像实例不可调用。
MyClass
now refers to the instance of what MyClass
previous held that is a class.
MyClass现在指的是MyClass之前持有的类的实例。
You could make a new instance of former MyClass
by:
您可以通过以下方式创建以前的MyClass的新实例:
new_class = MyClass.__class__()
MyClass
is just just a variable that points/refers to a particular object. First it was class then it was changed to hold an instance of that class
.
MyClass只是一个指向/引用特定对象的变量。首先它是类,然后它被更改为持有该类的实例。
#4
-1
Variables are treated as objects in Python. From my understanding, when you assign a new instance of MyClass to an object, python will try to create a reference of the original class to the object and duplicate. However, the namespace of the new object is already used (in the original MyClass), and the duplication will return you an error, so the first code will not work.
变量在Python中被视为对象。根据我的理解,当您将一个新的MyClass实例分配给一个对象时,python将尝试创建一个原始类的引用到该对象并复制。但是,新对象的命名空间已经被使用(在原始的MyClass中),并且复制将返回错误,因此第一个代码将不起作用。
For the second piece of code, the final line will not execute due to the same reason of Namespace Duplication. Since the last but one line failed, the proposed reference target is still the original MyClass, which won't work at all.
对于第二段代码,由于名称空间复制的相同原因,最后一行将不会执行。由于最后一行但是失败了,建议的参考目标仍然是原始的MyClass,它根本不起作用。
#1
3
class MyClass:
pass
MyClass=MyClass()
In simple terms, the above code does three things (in this order):
简单来说,上面的代码做了三件事(按此顺序):
-
Defines the class
MyClass
.定义类MyClass。
-
Creates an instance of
MyClass
.创建MyClass的实例。
-
Assigns that instance to the variable
MyClass
.将该实例分配给变量MyClass。
After the last step, the class MyClass
is overwritten and can no longer be used. All you have left is an instance of it contained in the variable MyClass
.
在最后一步之后,类MyClass被覆盖,无法再使用。你剩下的就是它包含在变量MyClass中的一个实例。
Moreover, if you try to call this instance as you would a class, you will get an error:
此外,如果您尝试像调用类一样调用此实例,则会出现错误:
>>> class MyClass:
... pass
...
>>> MyClass=MyClass()
>>> new_class=MyClass()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'MyClass' object is not callable
>>>
#2
4
The right-hand side of the assignment is processed first, so an instance of MyClass
is created. But then you reassign the name MyClass
to that instance. When you execute
首先处理赋值的右侧,因此创建了MyClass的实例。但是,您将名称MyClass重新分配给该实例。当你执行
new_class = MyClass()
you should get an error about MyClass
not being callable, since that name now refers to an instance of the original class, not the class itself.
你应该得到一个关于MyClass不可调用的错误,因为该名称现在指的是原始类的实例,而不是类本身。
#3
2
The line:
这条线:
new_class=MyClass()
in most cases will return an error, saying something like instance not callable.
在大多数情况下会返回一个错误,说像实例不可调用。
MyClass
now refers to the instance of what MyClass
previous held that is a class.
MyClass现在指的是MyClass之前持有的类的实例。
You could make a new instance of former MyClass
by:
您可以通过以下方式创建以前的MyClass的新实例:
new_class = MyClass.__class__()
MyClass
is just just a variable that points/refers to a particular object. First it was class then it was changed to hold an instance of that class
.
MyClass只是一个指向/引用特定对象的变量。首先它是类,然后它被更改为持有该类的实例。
#4
-1
Variables are treated as objects in Python. From my understanding, when you assign a new instance of MyClass to an object, python will try to create a reference of the original class to the object and duplicate. However, the namespace of the new object is already used (in the original MyClass), and the duplication will return you an error, so the first code will not work.
变量在Python中被视为对象。根据我的理解,当您将一个新的MyClass实例分配给一个对象时,python将尝试创建一个原始类的引用到该对象并复制。但是,新对象的命名空间已经被使用(在原始的MyClass中),并且复制将返回错误,因此第一个代码将不起作用。
For the second piece of code, the final line will not execute due to the same reason of Namespace Duplication. Since the last but one line failed, the proposed reference target is still the original MyClass, which won't work at all.
对于第二段代码,由于名称空间复制的相同原因,最后一行将不会执行。由于最后一行但是失败了,建议的参考目标仍然是原始的MyClass,它根本不起作用。