πŸ“Functions as Objects#

Let’s build a function to draw a drexel dragon.

def drexel_dragons():
    print(
        """         

           /\_/\   _   ()_()  wWw  wW    Ww wWw  W  W        _   ()_()            \/       .-.   \\\  ///  oo_    
          / o o \   /||_ (O o)  (O)_(O)\  /(O)(O)_(O)(O)      /||_ (O o)    /)     (OO)    c(O_O)c ((O)(O)) /  _)-< 
     /\   \~(*)~/     /o_) |^_\  / __)`. \/ .' / __) ||         /o_) |^_\  (o)(O) ,'.--.)  ,'.---.`, | \ ||  \__ `.  
     //\\  /     \   / |(\ |(_))/ (     \  /  / (    | \       / |(\ |(_))  //\\ / /|_|_\ / /|_|_|\ \||\\||     `. | 
     //  \\(       )  | | ))|  /(  _)    /  \ (  _)   |  `.     | | ))|  /  |(__)|| \_.--. | \_____/ ||| \ |     _| | 
     //    \/\~*.*~/   | |// )|\\ \ \_  .' /\ `.\ \_  (.-.__)    | |// )|\\  /,-. |'.   \) \'. `---' .`||  ||  ,-'   | 
     //__/\_/   \      \__/ (/  \) \__)(_.'  `._)\__)  `-'       \__/ (/  \)-'   ''  `-.(_.'  `-...-' (_/  \_)(_..--'  """
    )

Let’s assign drexel_dragons to a variable Penn

Penn = drexel_dragons

Why did this not run?

We just assigned this to a variable that is an object of the function drexel_dragons

We can discover what the object is by printing the type

print(type(Penn))
<class 'function'>

Or running it by calling the function

Penn()
         

           /\_/\   _   ()_()  wWw  wW    Ww wWw  W  W        _   ()_()            \/       .-.   \\  ///  oo_    
          / o o \   /||_ (O o)  (O)_(O)\  /(O)(O)_(O)(O)      /||_ (O o)    /)     (OO)    c(O_O)c ((O)(O)) /  _)-< 
     /\   \~(*)~/     /o_) |^_\  / __)`. \/ .' / __) ||         /o_) |^_\  (o)(O) ,'.--.)  ,'.---.`, | \ ||  \__ `.  
     //\  /     \   / |(\ |(_))/ (     \  /  / (    | \       / |(\ |(_))  //\ / /|_|_\ / /|_|_|\ \||\||     `. | 
     //  \(       )  | | ))|  /(  _)    /  \ (  _)   |  `.     | | ))|  /  |(__)|| \_.--. | \_____/ ||| \ |     _| | 
     //    \/\~*.*~/   | |// )|\ \ \_  .' /\ `.\ \_  (.-.__)    | |// )|\  /,-. |'.   \) '. `---' .`||  ||  ,-'   | 
     //__/\_/   \      \__/ (/  \) \__)(_.'  `._)\__)  `-'       \__/ (/  \)-'   ''  `-.(_.'  `-...-' (_/  \_)(_..--'  

Reassigning Functions#

We can assign functions to a different variable

drexel = drexel_dragons
drexel is Penn
True
drexel == Penn
True

Functions in Data Structures#

Data Structures are objects that store other objects: List, Tuples, Dictionaries, Sets

Since functions are objects, they can be stored in data structures.

[Penn, drexel]
[<function __main__.drexel_dragons()>, <function __main__.drexel_dragons()>]

Functions calling Functions#

Functions can be called by other functions. A good example of this is the function map

map() function returns a map object (which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.).

import numpy as np
import matplotlib.pyplot as plt


def sig(x):
    return 1 / (1 + np.exp(-x))
x = np.random.uniform(-5, 5, 100)
plt.plot(x, np.array(list(map(sig, x))), "s")
[<matplotlib.lines.Line2D at 0x7fca0073fa30>]
../../_images/84391a12e3d47f5e992dfa4079d5db0ece6fc090e4633ab0bf25cae99eb01b62.png

Function Scope#

A variable is only available from inside the region in which it was created.

Local Scope#

A variable created inside a function belongs to the local scope of that function and can only be used inside that function.

A variable created inside a function is available inside that function:

def myfunc():
    x = 300
    print(x)
    

myfunc()
300

Global Scope#

A variable created in the main body of the Python code is a global variable and belongs to the global scope.

Global variables are available from within any scope, global and local.

A variable created outside of a function is global and can be used by anyone:

x = 300


def myfunc():
    print(x)


myfunc()

print(x)
300
300

Common Mistakes#

You cannot access a locally defined variable from outside of a function, as it does not exist

x = 300


def my_function():
    print(x)
    y = 200
    print(y)


y