How are we able to call functions before they are defined in Python?
Question:
In Python functional programming this code is valid and runs as expected:
def increment_5(num):
number = 5
number += num
disp_result(num)
def disp_result(num):
print(f"5 was increased by {num}")
increment_5(6) # -> 5 was increased by 6
I have created the disp_result()
function after calling it. I have seen in other people’s code (and I write in this way too) that they call functions after defining them and I believe this is the convention and good practice. But, as shown, we can also call a certain function before actually defining it. In OOP too, I can call a class method using self.certain_method()
before acutally defining this method. So how is it achieved and is it something that happens in Python only?
I understand this question could have been asked before. If such, please know that I was unable to find it and linking to it would be helpful too.
Answers:
You can’t call a function before it’s defined. disp_result
isn’t called until increment_5
is called.
For example, this will fail:
def increment_5(num):
number = 5
number += num
disp_result(num)
increment_5(6) # -> NameError: name 'disp_result' is not defined
def disp_result(num):
print(f"5 was increased by {num}")
As opposed to classes, function bodies are not executed without explicitly calling the function. Follow along:
>>> class A:
... print('hello')
...
hello
>>> def f():
... print('hello')
...
# <nothing happens>
Since Python does not execute the function body until you call that function, you can populate the scopes where that function might look for specific names after defining it.
The reason why code such as this works:
class Foo():
def method1(self):
self.method2()
def method2(self):
print("hello")
f = Foo()
f.method1()
is that the variable self.method2
inside Foo.method1
is only evaluated (and the function to which it points only gets called) at the time that the code inside Foo.method1
is actually executed, not while it is being defined.
Before class Foo
can be instantiated, the class definition must first have completed (i.e. the class object must have been created and it must have been populated with all of its methods). Therefore at the time when the instance f
exists and f.method1()
(or equivalently, Foo.method1(f)
) is called, the instance object (passed to the method via its self
argument) already has a property method2
inherited from the class, which has by that time been fully populated.
It therefore does not matter in which order methods are declared within a class.
This is a completely different situation from trying to call a function before it has been defined.
In Python functional programming this code is valid and runs as expected:
def increment_5(num):
number = 5
number += num
disp_result(num)
def disp_result(num):
print(f"5 was increased by {num}")
increment_5(6) # -> 5 was increased by 6
I have created the disp_result()
function after calling it. I have seen in other people’s code (and I write in this way too) that they call functions after defining them and I believe this is the convention and good practice. But, as shown, we can also call a certain function before actually defining it. In OOP too, I can call a class method using self.certain_method()
before acutally defining this method. So how is it achieved and is it something that happens in Python only?
I understand this question could have been asked before. If such, please know that I was unable to find it and linking to it would be helpful too.
You can’t call a function before it’s defined. disp_result
isn’t called until increment_5
is called.
For example, this will fail:
def increment_5(num):
number = 5
number += num
disp_result(num)
increment_5(6) # -> NameError: name 'disp_result' is not defined
def disp_result(num):
print(f"5 was increased by {num}")
As opposed to classes, function bodies are not executed without explicitly calling the function. Follow along:
>>> class A:
... print('hello')
...
hello
>>> def f():
... print('hello')
...
# <nothing happens>
Since Python does not execute the function body until you call that function, you can populate the scopes where that function might look for specific names after defining it.
The reason why code such as this works:
class Foo():
def method1(self):
self.method2()
def method2(self):
print("hello")
f = Foo()
f.method1()
is that the variable self.method2
inside Foo.method1
is only evaluated (and the function to which it points only gets called) at the time that the code inside Foo.method1
is actually executed, not while it is being defined.
Before class Foo
can be instantiated, the class definition must first have completed (i.e. the class object must have been created and it must have been populated with all of its methods). Therefore at the time when the instance f
exists and f.method1()
(or equivalently, Foo.method1(f)
) is called, the instance object (passed to the method via its self
argument) already has a property method2
inherited from the class, which has by that time been fully populated.
It therefore does not matter in which order methods are declared within a class.
This is a completely different situation from trying to call a function before it has been defined.