Jack_of_All_Trades
Jack_of_All_Trades

Reputation: 11468

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. 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. 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?

class MyClass:
      pass

MyClass=MyClass()
new_class=MyClass()

Upvotes: 2

Views: 1049

Answers (4)

user2555451
user2555451

Reputation:

class MyClass:
    pass

MyClass=MyClass()

In simple terms, the above code does three things (in this order):

  1. Defines the class MyClass.

  2. Creates an instance of MyClass.

  3. Assigns that instance to the variable 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.

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
>>>

Upvotes: 3

thepusher
thepusher

Reputation: 9

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.

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.

Upvotes: -1

Bleeding Fingers
Bleeding Fingers

Reputation: 7129

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.

You could make a new instance of former MyClass by:

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.

Upvotes: 2

chepner
chepner

Reputation: 530853

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

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.

Upvotes: 4

Related Questions