P
P
PesyCorm2020-12-04 13:45:31
Python
PesyCorm, 2020-12-04 13:45:31

How do classes work in Python?

What is the order of execution of classes in python code?
Well, that is, take a class and a function as a comparison

class Test_class:
    print(123)


def test_def():
    print(789)
As a result of the execution, it turns out that the class executed the command print, but the function did not. If I call a function, it will execute it. Shouldn't a class also work like a function after calling it? Or, it turns out that first the class is executed (when the program starts), and a little later in the code I will turn to it with a specific request and it will be executed again?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dooMoob, 2020-12-04
@dooMoob

the class did not execute any command, the command was executed by the interpreter.
And you don't call the function, you call it. Calling a class will in turn call its constructor, which will return an instance of that class.

W
Wataru, 2020-12-04
@wataru

https://docs.python.org/3/tutorial/classes.html#cl... - statements inside a class are executed once.
Usually, definitions of methods and class members are written there. In the same way as you have written the definition of the test_def function in the code below. The definition is always executed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question