use of function , in python decorator - python

can someone explain how this code is working?
I mean I didn't execute the inner() function after I defined it.
So how was it executed?
I mean I did execute the hi() function, but how does it translate to the inner() function being executed?
def outer(msg):
text = msg
def inner():
print(text)
return inner
hi = outer('hi')
hi()

Your outer function returns inner. Hence hi = inner, and when you call hi() you're calling inner().

You have to realise that functions often have a return at the end. In this case your outer() function has: return inner which is the inner function defined there.
Also you must know that when you call a function which has a return, you can get hold of what is returned by assigning a variable to take that value.
Your code does this also with: hi = outer('hi'). Now your hi variable holds the reference to inner, which being a function can be called.
Your code does this also with: hi() which invokes inner() which executes and calls print(text).

The function outer is basically building a new function called inner (with a proloaded text message), and returning its handle. This construction is sometimes called "factory", because you are creating a new function with pre-loaded arguments.
The code is perfectly functional. However, it's usually considered better to define a class initialized with all the desired parameters, such as text. This helps others to understand the code better, and it looks more professional.
PS. Beware that factory functions, such as outer->inner, can easily introduce bugs. For example, consider this code:
def outer(msg):
text = msg
def inner():
print(text)
text = 'bug'
return inner
hi = outer('hi')
hi()
# prints "bug"
The meaning of text was changed after the function inner was created, but it was still affected. This is because the variable text is read from the dictionary of variables defined for the parent function (outer) at runtime. Thus, in complex applications, it can be stressing to ensure that a function such as inner is working properly.

Related

Calling variables in Python (local vs global)

The below works and gives 8:
def mypow(a,b):
c=a**b
print (c)
mypow(2,3)
But the below doesn't:
def mypow(a,b):
c=a**b
mypow(2,3)
I understand that in the former we print but since the latter doesn't give an error message (nor does it give a result), what is actually happening in the latter?
in
def mypow(a,b):
c=a**b
mypow(2,3)
after function executed; all data inside the function destroy. if you want to see anything on your screen after function is executed; either use print or return.
In the end of any function you have multiple choices.
1- return. return the output. when you return the output you receive it after function executed without error eg: d = func(any_value), if you specified the return value in func you will receive it and store it in d. if you don't specify the return statement in the function the function will return None.
2- print. print anything. this is what you did in your first function. in this case you get printed stuff in your screen, and you function return None, so you can't hold the output (a printed value) in any variable or use it anywhere else.
3- global. assign to global. when you need this option you create a variable outside of your function say my_variale=5, and in the first line of your function you write gloabl my_variable. say the function contain 5 lines, in line #3 you did my_variable=7. after function is executed and destroyed all values within the function; when you check my_variable you will see it contains value of 5.
You can replace print with return for your function and get the same result
def mypow(a,b):
c=a**b
return c
mypow(2,3)
Variables nested in functions are treated as local variables unless you add global before the variable name.

Why is there no error when the returned function is not being assigned?

I'm learning the concept of first class functions in Python (using Python 3.6.8) and couldn't figure out why code below doesn't show any errors.
def outer_fn(msg):
def inner_fn():
print(msg)
return inner_fn
outer_fn("text")
Because it's a perfectly fine thing to do.
Sometimes you call a function for its side effects (it prints something, saves something to a database, changes some variable) and aren't interested in the return value. Python does not tell you to do anything with the return value, it just calls the function for you.
It's not showing an error because your code doesnt have any errors,you need to call your inner function like:
outer = outer_fn("text")
outer() # call inner_fn
full code:
def outer_fn(msg): #outer function
def inner_fn(): #inner function
print(msg) #able to acces the variable of outer function
return inner_fn
outer = outer_fn("text")
outer() # call inner_fn
output:
text

Python call inner function

I have the following problem:
I have a function that holds another function inside (see code bellow). I need to call the function getFastaEntry with a parameter filename set in the top level function.I tried to call it with generate_fasta_reader(filename).getFastaEntry(), but I get the error message "function object has no attribute getFastaEntry()". Can anybody tell me how I can call the inner function?
def generate_fasta_reader(filename, gzipped=False, strip=True):
def getFastaEntry():
filehandle = None
You must return the inner function. It's not accessible otherwise and in fact ceases to exist as soon as the function ends, because there are no references to it at that point. The parameters of the outer function are available to the inner function via a closure. (I assume your actual getFastaEntry() function actually does something besides assign a local variable that goes out of scope almost immediately.)
def generate_fasta_reader(filename, gzipped=False, strip=True):
def getFastaEntry():
filehandle = None
return getFastaEntry
Calling:
getter = generate_fasta_reader("foo.txt")
getter()
Or in one step (if you only need it call the inner function once per outer function call):
generate_fasta_reader("foo.txt")()

global name is not defined even though it is? [duplicate]

I am new to Python so please don't flame me if the question is too basic :)
I have read that Python is executed from top - to - bottom.
If this is the case, why do programs go like this:
def func2():
pass
def func1():
func2()
def func():
func1()
if __name__ == '__main__':
func()
So from what I have seen, the main function goes at last and the other functions are stacked on top of it.
Am I wrong in saying this? If no, why isn't the main function or the function definitions written from top to bottom?
EDIT: I am asking why I can't do this:
if __name__ == '__main__':
func()
def func1():
func2()
Isn't this the natural order? You keep on adding stuff at the bottom, since it is executed from top to bottom.
The defs are just creating the functions. No code is executed, other than to parse the syntax and tie functions to those names.
The if is the first place code is actually executed. If you put it first, and call a function before it is defined, the result is a NameError. Therefore, you need to put it after the functions are defined.
Note that this is unlike PHP or JavaScript, where functions are 'hoisted' - any function definitions are processed and parsed before everything else. In PHP and JavaScript, it's perfectly legal to do what you are saying and define functions in the source lower down than where they are called. (One detail in JS is that functions defined like function(){} are hoisted, while functions defined like var func1=function(){}; are not. I don't know how it works with anonymous functions in PHP 5.3 yet).
See, in this, cat() will print correctly, and yip() gives you a NameError because the parser hasn't gotten to the definition of yip() at the time you call it.
def cat():
print 'meowin, yo'
cat()
yip()
def yip():
print 'barkin, yall'
meowin, yo
Traceback (most recent call last):
File "cat.py", line 5, in
yip()
NameError: name 'yip' is not defined
Python is executed from top to bottom, but executing a "def" block doesn't immediately execute the contained code. Instead it creates a function object with the given name in the current scope. Consider a Python file much like your example:
def func2():
print "func2"
def func1():
func2()
def func():
func1()
if __name__ == '__main__':
func()
What happens when this script is executed is as follows:
Firstly, a function object is created and bound to the name "func2" in the global scope. Then a function object is created and bound to the name "func1" in the global scope. Then one called "func". Then the "if" statement is executed, the condition is true and the "func()" statement is executed. At this point "func" is a function object found in the global scope, so it is invoked and its code runs. That code contains the "func1()" statement, which is resolved to a call to the "func1" function, and so on.
If you put the "if" statement at the top then when it executes there would not yet be anything defined with the name "func", so you would get an error. The important thing to recognise is that the "def" statement is itself a statement that is executed. It is not like in some other languages where definitions are a separate sort of declaration with no order of execution.
Also note that so long as the "if __name__..." bit is at the end of the file, it doesn't really matter what order the other declarations are in, since by the time any of them are called all of the "def"s will have already been executed.
Python does, in general, process commands from top to bottom. However, a function call will cause Python to execute that function, and continue downward only after that call has ended.
In your example, the Python interpreter executes the following steps:
Define func2.
Define func1.
Define func.
Process if statement if __name__ == '__main__':.
Call the func function (since the condition is true).
Call the func1 function (because that's what func does).
Call the func2 function (because that's what func1 does).
End, because after finishing the call to func2 it has also finished calling func1 and therefore has finished calling func, which was the final statement in the code.
It's also worth noting that you can have function calls written occur "before they're defined", as long as they aren't executed. Mentioning this here as it's a common new-to-python error. None of the other examples demonstrate this "it's ok, in one way" behavior. (though not really recommended)
def hi():
pie()
#hi() cannot be called here, doesn't work because hi depends on pie
def pie():
print('hi pie')
hi() # it's ok to call here!
You can even go a little further (slightly silly example, but I believe it clarifies the example)
runIt = False
def hi():
if runIt:
pie()
hi() #now ok, as the code won't call pie.
runIt = True
#hi() #wouldn't be ok as now the code will call pie.
def pie():
print('hi pie')
hi() # ok here too!
The if __name__ == "__main__" part goes at the end, because presumably whatever your main function does will need all the other definitions in the script. If there were any other function definitions below the main block, then it would not be possible for them to be used in there, since they haven't been seen by the interpreter at that point.
def statements simply define a function - they don't actually run it until the function is invoked. Hence why they generally come before whatever code uses them - they set the function up to be used in the future.
There's no hard requirement that def statements come before anything else, it's just fairly common to put the __main__ code at the bottom (among other things, it makes it clear what's in that section and what isn't, since anything below the if is part of that section). It also makes sure whatever you want to call from the section has already been def'd.
For the second part of your question (Style of coding in Python), I would take a look at PEP 8, which provides a style guide written by Guido van Rossum (the BFDL himself!) and provides the basics of writing Pythonic code.
It's true that the convention is to put the "main" function just above the "if __name__ == '__main__'" statement at the end of the file. I think that this is because we usually want functions to be near to their calling sites.
As for the called function coming above the calling function, I think that this is just a result of refactoring. At least in my case, I tend to write a function, and then take out bits of code to put into sub-functions, which I put on top so that I can see better.
This is just a primitive form of code organization. If there's a stronger cohesive unit, I'd be tempted to pull those functions into their own module.
There is two points that you need to know :
The main condition is to block the prevent code from being run when
the module is imported. In our case, the condition is true because
our file is not imported.
The code is working as follows :
Define func2
Define func1
Define func
Process if statement if __name__ == '__main__': (which in our case is True)
Call the func function
Call the func1
Call the func2 function
return the result(print the message func2)
End

In python, when you pass internally defined functions into other functions, how does it keep the variables?

For example, why does this work?
def func1(func1var):
def innerfunc(innerfuncvar):
if func1var == 1:
print innerfuncvar
else:
print 5
func2(innerfunc)
def func2(function):
function(9)
When innerfunc is called in func2, how does it know the values of func1var?
You've created a closure. Basically, think of it like this, from the point of view of the inner function:
func1var = whatever
def func2(function):
function(9)
def innerfunc(innerfuncvar):
if func1var = 1:
print innerfuncvar
else:
print 5
func2(innerfunc)
It doesn't care whether func1var is in an outer or global scope -- it just looks for it in each scope outward, starting with it's own local scope. It's just like when you reference a module global variable from inside a class or function in that module.
Python actually goes to some lengths to do this for you. When you define a function inside a function, you may use variables from the outer function in the inner function. This is true for any depth of nesting (that is, you can have a function inside a function inside a function inside a function... to any depth... and the innermost function can use variables from any of the enclosing functions). If there are conflicting names, the innermost variable with the name requested is used.
What's more, Python actually captures any variables you use from the outer function and stores them in the inner function, which is called a closure. So you can not only pass a function to another function, as you are doing, but you can return a function from a function, and variables you use from the outer function that were in effect when the function was defined will still be accessible from the returned function, even though the outer function isn't running any more. This is a fairly advanced feature, but it lets you do something like this:
def make_adder(increment):
def adder(number):
return number + increment
adder.__name__ = "adder(%s)" % increment
return adder
This function is a function that creates a function that adds the specified value to it. For example:
add1 = make_adder(1)
add5 = make_adder(5)
print add1(10) # 11
print add5(10) # 15
In this case, the value you pass to make_adder is captured and stored in the function that gets returned. This allows you to create a bunch of functions that add any number to their arguments. Which is a trivial example that isn't actually very useful in real life, but serves to illustrate the feature.
Each time you call func1(func1var), Python actually builds a new function innerfunc(). Since func1var is perfectly defined when innerfunc() is created, the code of the new innerfunc() function contains the correct value of func1var.

Categories