Eine ausführliche Beschreibung finden Sie hier: https://www.python-kurs.eu/klassen.php
Wir haben bereits vordefinierte Objekte und Methoden von Klassen benutzt.
liste1 = [1,2,3]
liste1 # liste1. + TAB Taste
a = -23
type(a)
dir(a)
Die minimale Definition einer Klasse in Python hat die Form
class ErsteKlasse(object):
pass # pass gibt an, dass hier noch irgendwas hin soll
p = ErsteKlasse()
p
class ZweiteKlasse(object):
""" eine zweite Klasse """
n = 1234
def f(x):
return x**2
z = ZweiteKlasse
z
dir(z)
class Student(object):
noten = [] #
def __init__(self,Vn,Nn,MatNr):
self.Vorname = Vn
self.Nachname = Nn
self.MatrikelNummer = MatNr
#'__init__ wird automatisch beim Erstellen eines Objektes aufgerufen
# Klassenmethoden
def notehinzufuegen(self,note):
self.noten.append(note)
def hallo(self):
print('Hallo mein Name ist {0} {1}'.format(self.Vorname,self.Nachname))
a = Student('Li','Ping',101)
b = Student('Lisa','Paul',102)
a.notehinzufuegen(1)
b.notehinzufuegen(4)
print('Hallo {0} {1}'.format(a.Vorname,a.Nachname))
a.hallo()
b.hallo()
a.noten
class Student(object):
def __init__(self,Vn,Nn,MatNr):
self.Vorname = Vn
self.Nachname = Nn
self.MatrikelNummer = MatNr
self.noten = []
def notehinzufuegen(self,note):
self.noten.append(note)
def hallo(self):
print('Hallo mein Name ist {0} {1}'.format(self.Vorname,self.Nachname))
a = Student('Li','Ping',101)
b = Student('Lisa','Paul',102)
a.notehinzufuegen(1)
b.notehinzufuegen(4)
a.noten, b.noten
(from Lantangen's book, page 419):
1) Any class method must have self as first argument. (The name can be any valid variable name, but the name self is a widely established convention in Python.)
2) self represents an (arbitrary) instance of the class.
3) To access any class attribute inside class methods, we must prefix with self, as in self.name, where name is the name of the attribute.
4) self is dropped as argument in calls to class methods.
class Student(object):
def __init__(self,Vn,Nn,MatNr):
self.Vorname = Vn
self.Nachname = Nn
self.MatrikelNummer = MatNr
self.noten = []
def saghallo(self):
print('Hallo mein Name ist {0} {1}'.format(self.Vorname,self.Nachname))
def brief(self):
return 'HALLO {0} Deine email ist {1}'.format(self.Vorname,self.email())
def email(self):
return '{0}.{1}@hhu.de'.format(self.Vorname,self.Nachname)
a = Student('Li','Ping',101)
b = Student('Lisa','Paul',102)
a.saghallo()
print(a.brief())
Eine Klasse für Polynome
class Polynom(object):
""" Polynomklasse Beschreibung .....
"""
def __init__(self,dp):
if isinstance(dp,(int,float,complex)):
dp = {0:dp}
self.dp = dp
self.degree = max(dp.keys())
def __repr__(self):
polystr = ''
for k in sorted(self.dp):
polystr = polystr + '{0:+g}*X^{1}'.format(self.dp[k],k)
return 'Polynom: ' + polystr
def __add__(self,other):
spow = set(self.dp.keys())
opow = set(other.dp.keys())
pows = spow.union(opow)
pps = dict()
for k in pows:
if k in spow:
if k in opow:
pps[k] = self.dp[k] + other.dp[k]
else:
pps[k] = self.dp[k]
else:
pps[k] = other.dp[k]
return Polynom(pps)
def __sub__(self,other):
""" Substraktion zweier Polynomen
"""
selfpow = set(self.dp.keys())
otherpow = set(other.dp.keys())
pows = selfpow.union(otherpow)
erg = dict()
for k in pows:
if k in selfpow:
if k in otherpow:
erg[k] = self.dp[k] - other.dp[k]
else:
erg[k] = self.dp[k]
else:
erg[k] = -other.dp[k]
return Polynom(erg)
def __call__(self,x):
""" Auswertung des Polynoms
"""
return sum([self.dp[k]*x**k for k in self.dp])
def __mul__(self,other):
""" Multiplikation eines Polynoms mit einem Skalar
TODO Polynommultiplikation
"""
erg = dict()
if isinstance(other,(int,float,complex)):
for k in self.dp:
erg[k] = self.dp[k] *other
return Polynom(erg)
__rmul__ = __mul__
pd = {0:1,10:2.5}
qd = {0:2/3,5:34}
p = Polynom(pd)
q = Polynom(qd)
p, q
print(p)
p+q
p-q
2*p
p*2
2*p+q
p(2)