How can Python call another function in a function regardless of the order of declaration?

Asked 2 years ago, Updated 2 years ago, 19 views

For example, for C,


int Fun1(){}
intFun2(){ //Fun2 goes up above Fun1 error
    Fun1();
}

Or,

int Fun1(); //declaration
int Fun2(); 

BLAH BLAH

int Fun1(){} // Implementation
int Fun2(){    
    Fun1();
}

It only works if it's done in a way.

But Python is


def Fun1():
    Fun2() #Order doesn't matter.

def Fun2():
   test

The light seems to be working smoothly.

It's actually a little hard to accept. I didn't tell you what kind of function it is and what form it is going to use, but you can use it without any basis.

How is this possible? I don't know what Fun2 is when Fun1 is declared.

Is it possible because the type check is done when driving? Because it's a medicine type? But at that time, I don't even know if there's a type that's not a problem.

Or is there another reason?

python

2022-09-21 21:11

2 Answers

Python does at runtime whether or not it has a symbol. This means that even if it is an invisible symbol in the code writing stage, you can think of it as accessible if the symbol is in memory while the code is running.

If the interpreter finds a symbol name as he executes the code line by line, it will take the object corresponding to the name from the symbol table and execute the code. Therefore, when you define a variable or function, the interpreter registers the symbol in the symbol table.

I revised and executed the code you mentioned as below. globals can see tables of symbols that exist across the globe.

print(globals())

def Fun1():
    print(globals())
    Fun2() #Order doesn't matter.

print(globals())

def Fun2():
    print("test")

Fun1()

The results are as follows.

{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__file__': '/home/deploy/src/solution.py', '__doc__': None, '__package__': None}
{'Fun1': <function Fun1 at 0x7fbced2515f0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': '/home/deploy/src/solution.py', '__package__': None, '__name__': '__main__', '__doc__': None}
{'Fun1': <function Fun1 at 0x7fbced2515f0>, 'Fun2': <function Fun2 at 0x7fbced251668>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': '/home/deploy/src/solution.py', '__package__': None, '__name__': '__main__', '__doc__': None}
test

Looking at the results, Fun1 and Fun2 do not exist in the symbol table before Fun1 is invoked. After def defines Fun1, you can see Fun1 in the symbol table. The same applies to Fun2.

If you look at the symbol table immediately after Func1 is called, you can ensure that Fun2 exists, and you can call it because it exists.

Of course, there is no Fun2 at the time Fun1 is defined, but if the symbol exists at the time the function is called, it can be called without any problems.

Conversely, if you remove Fun2 from the symbol table, it is as follows.

print(globals())

def Fun1():
    print(globals())
    Fun2() #Order doesn't matter.

print(globals())

def Fun2():
    print("test")

del globals()["Fun2"]
Fun1()
Traceback (most recent call last):
File "/solution.py", line 15, in <module>
Fun1()
File "/solution.py", line 7, in Fun1
Fun2() #Order doesn't matter.
NameError: global name 'Fun2' is not defined

As I mentioned above, because the symbol is found at the point of execution, if Fun1 is deleted before calling Fun1, you can see that Fun2 cannot be found.

Python looks for symbols when they are executed, not when they are defined, so you can use symbols that are not previously defined.

This is more about scope than about type-related attributes. It will be more helpful if you check the link.


2022-09-21 21:11

Your superiors are very good at organizing!!.

I will leave an address for another problem, Delegation, related to the question you asked.

Kind of like deep learning lol;

Language support for delegation

In languages that support delegation via method lookup rules, method dispatching is defined the way it is defined for virtual methods in inheritance: It is always the most specific method that is chosen during method lookup. Hence it is the original receiver entity that is the start of method lookup even though it has passed on control to some other object (through a delegation link, not an object reference).

Delegation has the advantage that it can take place at run time and affect only a subset of entities of some type and can even be removed at run time. Inheritance, by contrast, typically targets the type rather than the instances, and is restricted to compile time. On the other hand, inheritance can be statically type-checked, while delegation generally cannot without generics (although a restricted version of delegation can be statically typesafe[6]). Delegation can be termed "run-time inheritance for specific objects."

Here is a pseudocode example in a C#/Java like language:

https://en.wikipedia.org/wiki/Delegation_pattern

https://en.wikipedia.org/wiki/Delegation_(object-oriented_programming)


2022-09-21 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.