Dateien hochladen nach „decorator“

Decorator Sourcen
This commit is contained in:
2024-10-18 05:01:01 +00:00
parent 37cbae2683
commit b913b2cb01
5 changed files with 73 additions and 0 deletions

12
decorator/fib.py Normal file
View 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
View 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

View 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
View 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()

View 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()