Δημοσιεύτηκε: 24 Μαρ 2011, 14:12
από b_real
ευχαριστω που αχοληθηκες
για τα ονοματα εχεις δικιο απλα ηταν σε στιγμες που ειχα φρικαρει κ εβαζα οτι να'ναι μονο κ μονο για να δω αν λειτουργουνε
Τα τρεχω σε python 3.1.2 και 2.6(αλλαζοντας τα print)
με βαση τον κωδικα που μου εστειλες,αν τρεξω αυτο:
Spoiler: show
Κώδικας: Επιλογή όλων

class animals:
posotita = 0
def __init__(self,onoma,xrwma,podia):
self.onoma=onoma
self.xrwma=xrwma
self.podia=podia
animals.posotita += 1

def __del__(self):
animals.posotita -=1
print ('phres podi')

@staticmethod
def howMany():
print ('We have {0:d} animals.'.format(animals.posotita))

pipi = animals('lala','lalo',4)
animals.howMany()

μου πεταει αυτο:
Spoiler: show
Κώδικας: Επιλογή όλων

We have 1 animals.
Exception AttributeError: "'NoneType' object has no attribute 'posotita'" in <bound method animals.__del__ of <__main__.animals object at 0x7fe8fc0dd890>> ignored

το ιδιο "Exception AttributeError:" πεταει στον κωδικα του andreas_g (απο την προηγουμενη σελιδα)αν δεν βαλω τα del zwo1 κλπ...δηλαδη και καλειται ο καταστροφεας και πεταει αυτο το Exception AttributeError:

ειδα οτι το τρεχεις σε interpreter.Αν το ετρεχες κανονικα μηπως σου πεταει κ σενα οτι κ σε μενα;;

επισης κανοντας cp απο το tutorial τρεχω το παρακατω:
Spoiler: show
Κώδικας: Επιλογή όλων

class Robot:
'''Represents a robot, with a name.'''
# Μια μεταβλητή κλάσης, που μετρά τον αριθμό των ρομπότ
population = 0
def __init__(self, name):
'''Initializes the data.'''
self.name = name
print('(Initializing {0})'.format(self.name))
#Όταν δημιουργείται το άτομο, προστίθεται το ρομπότ στον
Robot.population += 1

def __del__(self):
'''I am dying.'''
print('{0} is being destroyed!'.format(self.name))
Robot.population -= 1
if Robot.population == 0:
print('{0} was the last one.'.format(self.name))
else:
print('There are still {0:d} robot working.'.format(Robot.population))

def sayHi(self):
'''Greeting by the robot.
Yeah, they can do that.'''
print('Greetings, my masters call me {0}.'.format(self.name))

def howMany():
'''Prints the current population.'''
print('We have {0:d} robots.'.format(Robot.population))
howMany = staticmethod(howMany)

droid1 = Robot('R2-D2')
droid1.sayHi()
Robot.howMany()
droid2 = Robot('C-3PO')
droid2.sayHi()
Robot.howMany()
print("\nRobots can do some work here.\n")
print("Robots have finished their work. So let's destroy them.")

Robot.howMany()

και εχω τα παρακατω αποτελεσματα:
Spoiler: show
Κώδικας: Επιλογή όλων

(Initializing R2-D2)
Greetings, my masters call me R2-D2.
We have 1 robots.
(Initializing C-3PO)
Greetings, my masters call me C-3PO.
We have 2 robots.

Robots can do some work here.

Robots have finished their work. So let's destroy them.
We have 2 robots.
R2-D2 is being destroyed!
There are still 1 robot working.
C-3PO is being destroyed!
C-3PO was the last one.

Αν δεν κανω λαθος δηλαδη παλι καλουνται οι καταστροφεις. :(

χιλια συγνωμη αν γινομαι κουραστικος απλα προσπαθω κ εγω να καταλαβω τι γινεται