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
def greet_repeatedly(num_times): | |
for i in range(num_times): | |
print('Hii') | |
return 'Hi, It\'s nice to see you!' |
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
# Function with one argument and returns string | |
def tell_me_your_age(age): | |
return f'I am {age} years old.' | |
# Function with one argument (string) and returns string | |
def introduce(name): | |
return f'HI... My name is {name} and nice to see you!' | |
# Function with one function as argument and returns its value | |
def introduce_john(func): |
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
def outer_function(): | |
def inner_function_one(): | |
print('Calling first inner function') | |
def inner_function_two(): | |
print('Calling second inner function') | |
inner_function_one() | |
inner_function_two() | |
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
# Simple calculator function with limited funcionalities | |
def simple_calculator(operation): | |
"""Returns operation we want, either add, subtract, multiply or divide""" | |
def add(x, y): | |
return x + y | |
def subtract(x, y): | |
return x - y |
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
def simple_decorator(func): | |
"""Takes function, modifies it and returns the wrapper""" | |
def wrapper(): | |
print("Before calling the function") | |
func() | |
print("After calling the function") | |
return wrapper |
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
def simple_decorator(func): | |
"""Takes function, modifies it and returns the wrapper""" | |
def wrapper(): | |
print("Before calling the function") | |
func() | |
print("After calling the function") | |
return wrapper | |
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
def do_ten_times(func): | |
"""Repeats func ten times""" | |
def wrapper(): | |
for i in range(10): | |
func() | |
return wrapper | |
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
@do_ten_times | |
def introduce_myself(name): | |
print(f"Hi, my name is {name}") | |
introduce_myself("Bob") |
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
def do_ten_times(func): | |
"""Repeats func ten times""" | |
def wrapper(): | |
for i in range(10): | |
func() | |
return wrapper | |
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
import functools | |
def do_ten_times(func): | |
"""Repeats func ten times""" | |
@functools.wraps(func) | |
def wrapper(): | |
for i in range(10): | |
func() |
OlderNewer