Order of execution with python class

橙三吉。 提交于 2020-08-08 05:38:39

问题


I was writing a small python script to understand a concept and got another confusion. Here's the code -

x = 5
y = 3

class Exp(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

        print("In",x, y, self.x, self.y)
    print("Middle",x,y)

print("Out",x,y)

Exp(1,2)

The output is -

Middle 5 3
Out 5 3
In 1 2 1 2

Now, my concept was python interpreter starts reading and executing the code from the first line to last line. It executes the code inside a class only when it is "called", not when it is defined. So, the output should print "Out" first. But here it is printing "Middle" first. This should not happen, as python interpreter when first encounters "Middle" - it is within the definition, and thus should not be executed at that time. It should be executed only after reading the last line of code where the class "Exp" is called.

I searched on Google and StackOverflow for the solution but couldn't find one explaining it for the class.

Kindly help me understand where I'm getting it wrong...


回答1:


Your doubt is right. I had the same doubt like 6 months back and a friend of mine helped me figure the answer out.

print("Middle",x,y)

The above statement does not belong to any method. It belongs to the class Exp. The __init__() method is executed when an object is created and is internally called by the Python interpreter when an object is instantiated from your end. Since the above statement is not a part of any method, the interpreter executes it before invoking the __init__ method. Since variables x and y are both available in the scope of class Exp, it isn't considered an error and the interpreter executes it.

If you remove the declarations of variables x and y, you will see a NameError like below.

Traceback (most recent call last):
  File "trial.py", line 9, in <module>
    print("Middle",x,y)
NameError: name 'x' is not defined

And this is because x and y are not even created from the class Exp's perspective.




回答2:


This odd behaviour happens because your print("Middle",x,y) is not inside a definition of a function, so it gets called before print("Out",x,y).

Your code is equivalent to:

x = 5
y = 3

class Exp(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

        print("In",x, y, self.x, self.y)

print("Middle",x,y)
print("Out",x,y) 
Exp(1,2)

Whose output will be:

Middle 5 3
Out 5 3
In 1 2 1 2

One of the possible ways to correct this is to define print("Middle",x,y), say within the constructor.

x = 5
y = 3

class Exp(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

        print("In",x, y, self.x, self.y)
        print("Middle",x,y)

print("Out",x,y) 
Exp(1,2)

The output you will then get is:

Out 5 3
In 1 2 1 2
Middle 1 2



回答3:


The issue is due to the print(middle) statement not being properly defined within the Exp class. When you run the code and the compiler runs through the script, it comes across this print(middle) line which is not defined as a method of the Exp class, but is executable as a built-in function, despite the whitespace.

If you try the code below, you get the output 'Out' and 'In'. You would only get 'Middle' by calling Exp.test()

x = 5
y = 3

class Exp(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

        print("In",x, y, self.x, self.y)

    def test(self):
        print("Middle",x,y)  

print("Out",x,y)

Exp(1,2)


来源:https://stackoverflow.com/questions/55793567/order-of-execution-with-python-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!