In Python Is there a C ++ equivalent for declaring a function and defining it after using it?

I apologize if this has already been answered. I really did not know how to look for this particular question.

In C ++, you can define a variable from above to define later. For instance:

int printOne();

int main()
{
     cout << printOne() << endl;
     return 0;
}

int printOne
{
     return 1;
}

I'm new to Python, so I was wondering if this is possible for Python.

+4
source share
2 answers

You do not need. Python evaluates everything at runtime:

def a():
    print(b())

def b():
    return 12

a()

therefore, when acalled b, it is already defined.

note: this does not work, because when a()called bis not yet defined:

def a():
    print(b())

a()

def b():
    return 12

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module1>", line 4, in <module>
  File "<module1>", line 2, in a
NameError: name 'b' is not defined
+4
source

, . . , , printOne main.

def main():
    print(printOne())

def printOne():
    return 1

main()
+1

Source: https://habr.com/ru/post/1668756/


All Articles