-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe3493b
commit 86d9548
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello, World!' | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# ----------- Python Functions as First Class Objects: Passing & Nesting Functions -------------- # | ||
|
||
# Functions can have inputs/functionality/output | ||
def add(n1, n2): | ||
return n1 + n2 | ||
|
||
def subtract(n1, n2): | ||
return n1 - n2 | ||
|
||
def multiply(n1, n2): | ||
return n1 * n2 | ||
|
||
def divide(n1, n2): | ||
return n1 / n2 | ||
|
||
# Functions are first-class objects, can be passed around as arguments e.g. int/string/float etc. | ||
|
||
def calculate(calc_function, n1, n2): | ||
return calc_function(n1, n2) | ||
|
||
result = calculate(add, 2, 3) | ||
print(result) | ||
|
||
# Functions can be nested in other functions | ||
|
||
def outer_function(): | ||
print("I'm outer") | ||
|
||
def nested_function(): | ||
print("I'm inner") | ||
|
||
nested_function() | ||
|
||
outer_function() | ||
|
||
# Functions can be returned from other functions | ||
def outer_function(): | ||
print("I'm outer") | ||
|
||
def nested_function(): | ||
print("I'm inner") | ||
|
||
return nested_function | ||
|
||
inner_function = outer_function() | ||
inner_function | ||
|
||
|
||
# Simple Python Decorator Functions | ||
import time | ||
|
||
def delay_decorator(function): | ||
def wrapper_function(): | ||
time.sleep(2) | ||
#Do something before | ||
function() | ||
function() | ||
#Do something after | ||
return wrapper_function | ||
|
||
@delay_decorator | ||
def say_hello(): | ||
print("Hello") | ||
|
||
# With the @ syntactic sugar | ||
@delay_decorator | ||
def say_bye(): | ||
print("Bye") | ||
|
||
# Without the @ syntactic sugar | ||
def say_greeting(): | ||
print("How are you?") | ||
decorated_function = delay_decorator(say_greeting) | ||
decorated_function() |