58 lines
1.1 KiB
Python
58 lines
1.1 KiB
Python
#Klassische Situation mit Klasse und Konstruktor __init__ ist dabei Konstruktor
|
|
#self ist vergleichbar mit this aus java
|
|
class Person:
|
|
def __init__(self, name, age):
|
|
self.name = name
|
|
self.age = age
|
|
def ausgabe(self):
|
|
print(self.name, self.age)
|
|
|
|
#Instanz von Person und anwenden der Funktion
|
|
p1 = Person("Peter", 30)
|
|
p1.ausgabe()
|
|
|
|
#Funktion anlegen ohne Typen
|
|
def rechne(a, b):
|
|
return a+b
|
|
|
|
#Funktion mit Typen
|
|
def rechne1(a: int, b: int) -> int:
|
|
return a+b
|
|
|
|
print(rechne(1, 2))
|
|
print(rechne1(1, 2))
|
|
|
|
#Python erlaubt Mehrfachvererbung
|
|
class A: pass
|
|
class B: pass
|
|
class C(A, B): pass
|
|
|
|
#Einfache Art und Weise der Nutzung von Lambdaausdrücken
|
|
square = lambda x: x*x
|
|
print(square(5))
|
|
|
|
def func(a,b):
|
|
return a+b
|
|
|
|
print(func(10, 2))
|
|
|
|
def func(a):
|
|
return a*a
|
|
|
|
#print(func(10, 2)) Dies funktioniert nicht, die die untere die obere überschreibt!
|
|
print(func(10))
|
|
|
|
#funktionen als objekte
|
|
def apply(func, value):
|
|
return func(value)
|
|
print(apply(lambda x: x+1, 5))
|
|
|
|
#enums
|
|
from enum import Enum
|
|
class Status(Enum):
|
|
OK = 1
|
|
NOT_FOUND = 2
|
|
ERROR = 3
|
|
|
|
print(Status.OK.value)
|
|
print(Status.OK) |