[Python] Which is executed first, the class variable or __init__?

I thought I was writing python. "Which of the class variables and \ _ \ _ init \ _ \ _ will be executed first ???" I don't dig deep. It is a small story. It's a waste to read until the end, so I'll write the result first.

result

Class variables are processed first, followed by \ _ \ _ init \ _ \ _.

Verification

Write print () in the class variable and \ _ \ _ init \ _ \ _ respectively. Check which one is displayed first from the execution result.

test.py


class main:

    s = 'Class variables have been defined.'
    print(s)

    def __init__(self):
        s = '__init__Was executed.'
        print(s)

if __name__ == '__main__':
    main()

result


$ python test.py
Class variables have been defined.
__init__Was executed.

The class variable is executed first, and then \ _ \ _ init \ _ \ _ is executed. Now let's define a class variable under \ _ \ _ init \ _ \ _. The code gets dirty, but it's a verification ...

test.py


class main:

    def __init__(self):
        s = '__init__Was executed.'
        print(s)

    s = 'Class variables have been defined.'
    print(s)


if __name__ == '__main__':
    main()

result


$ python test.py
Class variables have been defined.
__init__Was executed.

The result has not changed. It turns out that whichever you write above is always executed from the class variable. That's all.

Postscript

In the comments

Try running it without calling main (). It will be an interesting result.

When I tried it, only the class variable part was executed. Please refer to the comments for the easy-to-understand explanation. You can see that you can refer to the processing in the class and the method without executing the class. I can't get out of a beginner for the rest of my life. But it's fun.

Recommended Posts

[Python] Which is executed first, the class variable or __init__?
Why is the first argument of [Python] Class self?
Which is better, PyPy or Python?
Which is faster, Python shuffle or sample?
Which is better, python standard input receiving input () or sys.stdin?
Which is the most popular python visualization tool after all?
__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
[Python] Is the zero but non-empty object Truthy or Fallsy?
[Beginners are worried] Which is better, Ruby, PHP or Python?
First Python 3 ~ The beginning of repetition ~
[Python] What is @? (About the decorator)
[python] What is the sorted key?
See python for the first time
Examine the object's class in python
What is the python underscore (_) for?
Determining which OS is running Python
The first step in Python Matplotlib
If Python is less than 3.3, use the -R option or the environment variable PYTHONHASHSEED = "random" for DoS countermeasures.
Python does not output errors or output just because the indent is misaligned
[Python] Determine if any coordinate point is inside or outside the polygon
Generate a first class collection in Python
Continuously play the first Python Sukusta MV
OR the List in Python (zip function)
Where is the python instantiation process written?
What is "mahjong" in the Python library? ??
MongoDB for the first time in Python
[python] [meta] Is the type of python a type?
[Python] Number of integer sequences of length n for which the sum is m