Dateien hochladen nach „decorator“
Decorator Sourcen
This commit is contained in:
12
decorator/fib.py
Normal file
12
decorator/fib.py
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
import sys
|
||||
|
||||
def fib(n):
|
||||
if n in [0,1]:
|
||||
return n
|
||||
else:
|
||||
return fib(n-1) + fib(n-2)
|
||||
|
||||
|
||||
print(fib(int(sys.argv[1])))
|
||||
|
8
decorator/func_param.py
Normal file
8
decorator/func_param.py
Normal file
@@ -0,0 +1,8 @@
|
||||
def add(x, y):
|
||||
return x + y
|
||||
|
||||
def calculate(func, x, y):
|
||||
return func(x, y)
|
||||
|
||||
result = calculate(add, 4, 6)
|
||||
print(result) # prints 10
|
12
decorator/nested_function.py
Normal file
12
decorator/nested_function.py
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
def print_message(message):
|
||||
"Umgebende Function"
|
||||
def message_sender():
|
||||
"Eingebettete Function"
|
||||
print(message)
|
||||
|
||||
message_sender()
|
||||
|
||||
print_message("Some random message")
|
||||
|
20
decorator/pass_func.py
Normal file
20
decorator/pass_func.py
Normal file
@@ -0,0 +1,20 @@
|
||||
def make_pretty(func):
|
||||
# define the inner function
|
||||
def inner():
|
||||
# add some additional behavior to decorated function
|
||||
print("I got decorated")
|
||||
|
||||
# call original function
|
||||
func()
|
||||
# return the inner function
|
||||
return inner
|
||||
|
||||
# define ordinary function
|
||||
def ordinary():
|
||||
print("I am ordinary")
|
||||
|
||||
# decorate the ordinary function
|
||||
decorated_func = make_pretty(ordinary)
|
||||
|
||||
# call the decorated function
|
||||
decorated_func()
|
21
decorator/simpledecorator.py
Normal file
21
decorator/simpledecorator.py
Normal file
@@ -0,0 +1,21 @@
|
||||
def make_pretty(func):
|
||||
# define the inner function
|
||||
def inner():
|
||||
# add some additional behavior to decorated function
|
||||
print("I got decorated")
|
||||
|
||||
# call original function
|
||||
func()
|
||||
# return the inner function
|
||||
return inner
|
||||
|
||||
# define ordinary function
|
||||
@make_pretty
|
||||
def ordinary():
|
||||
print("I am ordinary")
|
||||
|
||||
# decorate the ordinary function
|
||||
decorated_func = make_pretty(ordinary)
|
||||
|
||||
# call the decorated function
|
||||
ordinary()
|
Reference in New Issue
Block a user