简体   繁体   English

类名作为python中的变量

[英]Class name as a variable in python

I was just looking at one question here and the OP was using a same name for class, other things and also for variable. 我只是在这里看一个问题,OP正在为类,其他东西以及变量使用相同的名称。 When I was trying to answer it, I became confused myself and thus thought of asking. 当我试图回答它时,我自己变得困惑,因此想到了问。

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. 我想了解python解释器将如何处理它。 So my question is, is the variable MyClass I will use will be created first or the other way? 所以我的问题是,我将使用的变量MyClass将首先创建还是以其他方式创建? Which is, creating an instance of MyClass firstly and assigning it to MyClass variable. 也就是说,首先创建一个MyClass实例并将其分配给MyClass变量。 I think the latter is correct but if that is the case, how will the following be resolved? 我认为后者是正确的,但如果是这样的话,将如何解决以下问题?

class MyClass:
      pass

MyClass=MyClass()
new_class=MyClass()

The right-hand side of the assignment is processed first, so an instance of MyClass is created. 首先处理赋值的右侧,因此创建了MyClass的实例。 But then you reassign the name MyClass to that instance. 但是,您将名称MyClass重新分配给该实例。 When you execute 当你执行

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不可调用的错误,因为该名称现在指的是原始类的实例,而不是类本身。

class MyClass:
    pass

MyClass=MyClass()

In simple terms, the above code does three things (in this order): 简单来说,上面的代码做了三件事(按此顺序):

  1. Defines the class MyClass . 定义类MyClass

  2. Creates an instance of MyClass . 创建MyClass的实例。

  3. Assigns that instance to the variable MyClass . 将该实例分配给变量MyClass

After the last step, the class MyClass is overwritten and can no longer be used. 在最后一步之后,类MyClass被覆盖,不能再使用了。 All you have left is an instance of it contained in the variable 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
>>>

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. MyClass只是一个指向/引用特定对象的变量。 First it was class then it was changed to hold an instance of that class . 首先它是类,然后它被更改为持有class的实例。

Variables are treated as objects in Python. 变量在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. 根据我的理解,当您将一个新的MyClass实例分配给一个对象时,python将尝试为该对象创建一个原始类的引用并复制。 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. 但是,新对象的命名空间已经被使用(在原始的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,它根本不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM