Please what does func() mean in python when used inside a function,For example in the code below.
def identity_decorator(func):
def wrapper():
func()
return wrapper
A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope. In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.
They can be created and destroyed dynamically, passed to other functions, returned as values, etc. Python supports the concept of a "nested function" or "inner function", which is simply a function defined inside another function.
Once we have defined a function, we can call it from another function, program, or even the Python prompt. To call a function we simply type the function name with appropriate parameters. >>> greet('Paul') Hello, Paul.
I was wondering the same! You can see how it works with the follow example:
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
pretty = make_pretty(ordinary)
pretty()
Output
I got decorated
I am ordinary
Now when you remove the func() and you try to rerun it:
def make_pretty(func):
def inner():
print("I got decorated")
return inner
def ordinary():
print("I am ordinary")
pretty = make_pretty(ordinary)
pretty()
Output
I got decorated
You see the the decorated function was not called. Please have a look here https://www.programiz.com/python-programming/decorator
func
is an argument given to the function identity_decorator()
.
The expression func()
means "call the function assigned to the variable func
."
The decorator is taking another function as an argument, and returning a new function (defined as wrapper
) which executes the given function func
when it is run.
Here is some information about decorators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With