Klassendefinitionen in Python
Funktionen mit internen Namen
Der Zähler einer Zählerfunktion kann nicht innerhalb der Zählerfunktion erzeugt werden, da die Bindungstabellen von Inkarnationen (Aufrufen einer Funktion) nach dem Ablauf der Inkarnation jedes Mal wieder verloren gehen.
main.py
def count():
value = -1
value += 1
return valueprint( count() )
print( count() )
print( count() )- Protokoll
0
0
0- gewünschte Ausgabe
0
1
2
Funktionen mit externen Namen
Es bleibt die Möglichkeit, den Zähler außerhalb der Funktion zu definieren, doch dies hat den Nachteil, daß die Funktionsdefinition dann nicht mehr alles enthält, das zur Funktion gehört, und Namenskonflikte mit anderen Variablen des Moduls auftreten können.
Das voranstehende Beispiel hat gezeigt, daß der Zähler immer wieder auf den Anfangswert zurückgesetzt wird, wenn die Initialisierung (Festlegung des Anfangswertes) mit der Inkrementierung (Erhöhung um eins) verbunden ist. Daher trennen wir beide voneinander, indem wir zwei Funktionen schreiben.
main.py
def init():
global value
value = -1def count():
global value
value += 1
return valueinit()
print( count() )
print( count() )
print( count() )- Protokoll
0
1
2
Mehrere Zähler
Es bleibt aber schwierig, mehrere Zähler anzulegen. Dazu muß anscheinend eine Kopie der Funktionen und der Variablen angelegt werden.
main.py
def init0(): global value0; value0 = -1
def count0(): global value0; value0 += 1; return value0def init1(): global value1; value1 = -1
def count1(): global value1; value1 += 1; return value1init0()
init1()print( count0() )
print( count1() )
print( count1() )
print( count1() )
print( count0() )- Protokoll
0
1
2
0
1
2
Klassendefinitionen
Eine Klassendefinition erlaubt es, Variablen und zugehörige Funktionen auf übersichtliche Weise in einem Objekt zusammenzufassen.
main.py
class C:
def init( object ):
object.value = -1
def count( object ):
object.value = -1def count():
global value
value += 1
return valueprint( count() )
print( count() )
print( count() )- Protokoll
0
1
2
Minimale Klassendefinitionen
Eine Klassendefinition beginnt mit »class« und ist eine Verbundanweisung.
main.py
class C():
passc = C()
Protokoll
- (keine Ausgabe)
PEP 8:Class names should normally use the CapWords convention.
- PEP 8
- Class names should normally use the CapWords convention.
- The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable.
- Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants.
Klassen für minimale Objekte
main.py
class C():
passc = C()
print( type( c ))
print( str( c ))
print( c )
print( repr( c ))
print( id( c ))
d = C()
print( c is d )
print( c == d )
- Protokoll
<class '__main__.C'>
<__main__.C object at 0x00000000021ACA00>
<__main__.C object at 0x00000000021ACA00>
<__main__.C object at 0x00000000021ACA00>
35310080
False
False