Olli Graf
1 month ago
9 changed files with 107 additions and 13 deletions
@ -0,0 +1,25 @@ |
|||||
|
# Datei: classdecor.py |
||||
|
|
||||
|
|
||||
|
def addrepr(cls): |
||||
|
# Universelle __repr__ Methode |
||||
|
def __repr__(self): |
||||
|
return f"{cls.__name__}({self.__dict__})" |
||||
|
cls.__repr__ = __repr__ |
||||
|
|
||||
|
return cls |
||||
|
|
||||
|
@addrepr |
||||
|
class Fahrzeug(): |
||||
|
def __init__(self,farbe,typ): |
||||
|
self.typ = typ |
||||
|
self.farbe = farbe |
||||
|
|
||||
|
|
||||
|
f1 = Fahrzeug('grau','VW') |
||||
|
f2 = Fahrzeug('rot','Ferrari') |
||||
|
|
||||
|
print(f'{f1}') |
||||
|
print(f'{f2}') |
||||
|
|
||||
|
|
@ -0,0 +1,13 @@ |
|||||
|
#Datei: counter.py |
||||
|
|
||||
|
def counter(func): |
||||
|
func.count = 0 |
||||
|
def wrapper(*args, **kwargs): |
||||
|
func.count = func.count +1 |
||||
|
print(f'{func.__name__} wurde {func.count}-mal aufgerufen.') |
||||
|
result = func(*args,**kwargs) |
||||
|
|
||||
|
return result |
||||
|
wrapper.count = 0 |
||||
|
return wrapper |
||||
|
|
@ -1,8 +1,16 @@ |
|||||
|
# Datei: func_param.py |
||||
|
|
||||
def add(x, y): |
def add(x, y): |
||||
return x + y |
return x + y |
||||
|
|
||||
|
def mul(x,y): |
||||
|
return x * y |
||||
|
|
||||
def calculate(func, x, y): |
def calculate(func, x, y): |
||||
return func(x, y) |
return func(x, y) |
||||
|
|
||||
result = calculate(add, 4, 6) |
result = calculate(add, 4, 6) # Aufruf von calculate mit add Funktion als Parameter |
||||
print(result) # prints 10 |
print(result) # Ausgabe ist 10 |
||||
|
|
||||
|
result = calculate(mul, 4, 6) # Aufruf von calculate mit add Funktion als Parameter |
||||
|
print(result) # Ausgabe ist 24 |
||||
|
@ -1,12 +1,12 @@ |
|||||
|
#Datei: nested_function.py |
||||
|
|
||||
def print_message(message): |
def print_message(message): |
||||
"Umgebende Function" |
print('Umgebende Funktion') |
||||
def message_sender(): |
def inner_function(): |
||||
"Eingebettete Function" |
print('Eingebettete Funktion') |
||||
print(message) |
print(message) |
||||
|
|
||||
message_sender() |
inner_function() |
||||
|
|
||||
print_message("Some random message") |
print_message("Irgendein Text") |
||||
|
|
||||
|
@ -0,0 +1,17 @@ |
|||||
|
|
||||
|
|
||||
|
def reverse_decorator(func): |
||||
|
|
||||
|
def wrapper(text): |
||||
|
make_reverse = "".join(reversed(text)) |
||||
|
return func(make_reverse) |
||||
|
|
||||
|
return wrapper |
||||
|
|
||||
|
@reverse_decorator |
||||
|
def format_message(text): |
||||
|
return f'Text: {text}' |
||||
|
|
||||
|
print(format_message('Hallo')) |
||||
|
|
||||
|
|
@ -0,0 +1,20 @@ |
|||||
|
# Datei static.py |
||||
|
|
||||
|
class Math(): |
||||
|
|
||||
|
@staticmethod |
||||
|
def add(x,y): |
||||
|
return x+y |
||||
|
|
||||
|
@staticmethod |
||||
|
def sub(x,y): |
||||
|
return x-y |
||||
|
|
||||
|
@staticmethod |
||||
|
def mul(x,y): |
||||
|
return x*y |
||||
|
|
||||
|
|
||||
|
print(f'Add: {Math.add(3,2)}') |
||||
|
print(f'Sub: {Math.sub(3,2)}') |
||||
|
print(f'Mul: {Math.mul(3,2)}') |
Loading…
Reference in new issue