< Section 11 | Section 12 >
Decorators with Python Overview
https://www.udemy.com/complete-python-bootcamp/learn/lecture/9497658#questions
There is A LOT going on here, so pay attention to the steps in order!
Decorators
Decorators allow you to ‘decorate’ or ‘wrap’ a function with additional code.
def simple_func():
# do some stuff
return something
Later you might want to add some new functionality to that existing function:
def simple_func():
# do some new stuff
# do some stuff
return something
- You can add the extra code (functionality) to the old function
- You can create a brand new function that contains the old code, and then add the new code to it
What if you want to remove the new functionality at a later date?
- You would need to delete it manually or not call the function
Python decorators allow you to tack on extra functionality to an already existing function. Decorators use the ‘@’ operator and are then placed on the top of the original function
@some_decorator
def simple_func():
# do some stuff
return something
Example:
def hello():
print("Hello!")
hello
<function __main__.hello()>
hello()
Hello!
Now create a ‘copy’ of hello by assigning it to ‘greet()’
greet = hello greet
<function __main__.hello()>
greet()
Hello!
Now prove it’s pointing to an object and not pointing to hello by deleting hello()
del hello hello()
NameError: name ‘hello’ is not defined
Prove greet still works
greet()
Hello!
greet
<function __main__.hello()>
Creating functions within functions and passing them
def hello(name='Sofia'):
print("You are in function hello()")
def lastname():
return 'Roberts'
def another_name():
return 'is not a Roberts'
print(lastname())
print(another_name())
print("I will return a function depending on your name.")
if name == 'Sofia':
return lastname
else:
return another_name
iam = hello('Sofia')
print(iam())
You are in function hello()
Roberts
is not a Roberts
I will return a function depending on your name.
Roberts
Same thing, short ans sweet.
def cool():
def super_cool():
return "I am super cool!"
return super_cool
whoami=cool()
— no output, just assigned whoami to super_cool —
print(whoami())
I am super cool!
Passing a function as an argument
Super basic
def hello():
print("Hello!")
def goodbye():
print("Goodbye!")
def other(some_defined_function):
some_defined_function()
# print the function location for good measure
print(some_defined_function)
other(hello)
other(goodbye)
Hello!
Goodbye!
Create a Decorator
def new_decorator(original_func):
'''
accepts an existing function and wraps it with additional code
'''
def wrap_func():
#add some optional starting code
print("Here is my new song, I hope you like it!")
# now run the original function
original_func()
#now add any remaining code
print("But some day I'm going to be an object!")
# return the 'new script'
return wrap_func
def my_original_function():
print("I'm just an old piece of code.")
#assign a new function name to the 'new' code
decorated = new_decorator(my_original_function)
# run the new function
decorated()
Here is my new song, I hope you like it!
I’m just an old piece of code.
But some day I’m going to be an object!
Now that we understand how they work, here is the shortcut version!
def new_decorator(original_func):
'''
accepts an existing function and wraps it with additional code
'''
def wrap_func():
#add some optional starting code
print("Here is my new song, I hope you like it!")
# now run the original function
original_func()
#now add any remaining code
print("But some day I'm going to be an object!")
# return the 'new script'
return wrap_func
@new_decorator
def my_original_function():
print("I'm just an old piece of code.")
# Run the original function
my_original_function()
Here is my new song, I hope you like it!
I’m just an old piece of code.
But some day I’m going to be an object!
Decorators Homework
https://www.udemy.com/complete-python-bootcamp/learn/lecture/3767592#questions
Flask: http://flask.pocoo.org/
- Good for creating web pages with Python
Django: https://www.djangoproject.com/
- A very popular web framework