Browse Source

Dateien hochladen nach „decorator“

Decorator Sourcen
master
raspithek 1 month ago
parent
commit
b913b2cb01
  1. 12
      decorator/fib.py
  2. 8
      decorator/func_param.py
  3. 12
      decorator/nested_function.py
  4. 20
      decorator/pass_func.py
  5. 21
      decorator/simpledecorator.py

12
decorator/fib.py

@ -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

@ -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

@ -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

@ -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

@ -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()
Loading…
Cancel
Save