From b913b2cb01533f4cc9f427719ce2d252fcebb1ff Mon Sep 17 00:00:00 2001 From: raspithek Date: Fri, 18 Oct 2024 05:01:01 +0000 Subject: [PATCH] =?UTF-8?q?Dateien=20hochladen=20nach=20=E2=80=9Edecorator?= =?UTF-8?q?=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Decorator Sourcen --- decorator/fib.py | 12 ++++++++++++ decorator/func_param.py | 8 ++++++++ decorator/nested_function.py | 12 ++++++++++++ decorator/pass_func.py | 20 ++++++++++++++++++++ decorator/simpledecorator.py | 21 +++++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 decorator/fib.py create mode 100644 decorator/func_param.py create mode 100644 decorator/nested_function.py create mode 100644 decorator/pass_func.py create mode 100644 decorator/simpledecorator.py diff --git a/decorator/fib.py b/decorator/fib.py new file mode 100644 index 0000000..6626a5e --- /dev/null +++ b/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]))) + diff --git a/decorator/func_param.py b/decorator/func_param.py new file mode 100644 index 0000000..76064b9 --- /dev/null +++ b/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 diff --git a/decorator/nested_function.py b/decorator/nested_function.py new file mode 100644 index 0000000..afac62f --- /dev/null +++ b/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") + diff --git a/decorator/pass_func.py b/decorator/pass_func.py new file mode 100644 index 0000000..eca999c --- /dev/null +++ b/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() diff --git a/decorator/simpledecorator.py b/decorator/simpledecorator.py new file mode 100644 index 0000000..f028fce --- /dev/null +++ b/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()