Skip to content

Instantly share code, notes, and snippets.

View agusrichard's full-sized avatar

Agus Richard Lubis agusrichard

View GitHub Profile
def greet_repeatedly(num_times):
for i in range(num_times):
print('Hii')
return 'Hi, It\'s nice to see you!'
# 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):
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()
# 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
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
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
def do_ten_times(func):
"""Repeats func ten times"""
def wrapper():
for i in range(10):
func()
return wrapper
@do_ten_times
def introduce_myself(name):
print(f"Hi, my name is {name}")
introduce_myself("Bob")
def do_ten_times(func):
"""Repeats func ten times"""
def wrapper():
for i in range(10):
func()
return wrapper
import functools
def do_ten_times(func):
"""Repeats func ten times"""
@functools.wraps(func)
def wrapper():
for i in range(10):
func()