Ich habe vor ein paar Tagen mit Python angefangen und habe mir als Einstieg vorgenommen mit dem Ebook Byteofpython anzufangen. Da bin ich jetzt bei den Klassen angelangt, aber ich komme gerade leider nicht weiter.
Das Beispiel mit der Klasse Person habe ich mal auf die Klasse Auto angepasst. Im Prinzip ist es genau das selbe, aber es funktioniert nicht.
Hier mal meine Klasse Auto:
Code: Alles auswählen
#!/usr/bin/python
#-*- coding: utf-8 -*-
class auto:
    anzahl = 0
    def __init__(self, name):
        self.name = name
        auto.anzahl += 1
    def __del__(self):
        print 'Das Auto %s ist schrott' % self.name
        auto.anzahl -= 1
        print 'Es gibt noch %s Auto(s)' % auto.anzahl
    def hupen(self):
        print '%s hupt' % self.name
    def zahl(self):
        print "Es gibt noch %s Auto(s)" % auto.anzahl
blau = auto('blau')
rot = auto('rot')
rot.hupen()
blau.hupen()
rot.zahl()
blau.zahl()Code: Alles auswählen
#!/usr/bin/python
class Person:
    bevoelkerung = 0
    def __init__(self, name):
        self.name = name
        Person.bevoelkerung += 1
    def __del__(self):
        print '%s verabschiedet sich.' % self.name
        Person.bevoelkerung -= 1
        print 'Es gibt noch %d Leute.' % Person.bevoelkerung
    def sagHallo(self):
        print 'Hallo, mein Name ist %s.' % self.name
    def wieViele(self):
        print 'Es gibt hier %d Leute.' % Person.bevoelkerung
swaroop = Person('Swaroop')
swaroop.sagHallo()
swaroop.wieViele()
kalam = Person('Abdul Kalam')
kalam.sagHallo()
kalam.wieViele()
swaroop.sagHallo()
swaroop.wieViele()Code: Alles auswählen
rot hupt
blau hupt
Es gibt noch 2 Auto(s)
Es gibt noch 2 Auto(s)
Das Auto blau ist schrott
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'anzahl'" in <bound method auto.__del__ of <__main__.auto instance at 0xb7d0d8cc>> ignored
Das Auto rot ist schrott
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'anzahl'" in <bound method auto.__del__ of <__main__.auto instance at 0xb7d0d9ac>> ignoredCode: Alles auswählen
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2MfG
Dr.ChAoS
PS: Ich habe mich als erstes mit dem namen "Dr.ChAoS" registriert, aber die bestätigungs Mail kam nicht an. Also ihr könnt den alten löschen.

 
 
