functions and functional programming if local variable is used before it' assigned valuean unboundlocalerror exception is raised here' an example that illustrates one scenario of how this might occuri def foo() print(iresults in unboundlocalerror exception in this functionthe variable is defined as local variable (because it is being assigned inside the function and there is no global statementhoweverthe assignment tries to read the value of before its local value has been first assigned even though there is global variable in this exampleit is not used to supply value here variables are determined to be either local or global at the time of function definition and cannot suddenly change scope in the middle of function for examplein the preceding codeit is not the case that the in the expression refers to the global variable iwhereas the in print(irefers to the local variable created in the previous statement functions as objects and closures functions are first-class objects in python this means that they can be passed as arguments to other functionsplaced in data structuresand returned by function as result here is an example of function that accepts another function as input and calls itfoo py def callf(func)return func(here is an example of using the above functionimport foo def helloworld()return 'hello worldfoo callf(helloworld'hello worldpass function as an argument when function is handled as datait implicitly carries information related to the surrounding environment where the function was defined this affects how free variables in the function are bound as an exampleconsider this modified version foo py that now contains variable definitionfoo py def callf(func)return func(nowobserve the behavior of this exampleimport foo def helloworld()return "hello world is %dx foo callf(helloworldpass function as an argument 'hello world is lib fl ff