raspithek
1 month ago
5 changed files with 73 additions and 0 deletions
@ -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]))) |
||||
|
|
@ -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 |
@ -0,0 +1,12 @@ |
|||||
|
|
||||
|
|
||||
|
def print_message(message): |
||||
|
"Umgebende Function" |
||||
|
def message_sender(): |
||||
|
"Eingebettete Function" |
||||
|
print(message) |
||||
|
|
||||
|
message_sender() |
||||
|
|
||||
|
print_message("Some random message") |
||||
|
|
@ -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() |
@ -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…
Reference in new issue