Δημοσιεύτηκε: 25 Μαρ 2011, 13:12
από b_real
για τον interpreter εννοουσα οτι δεν ανοιγω κονσολα παταω python κ αρχιζω κ γραφω κωδικα...
Αυτο που κανω ειναι να το γραφω σε Kate ή Komodo edit κ μετα να ανοιγω terminal και να δινω 'python3 ... .py'
το εχω δοκιμασει σε ubuntu 10.10 και σε Os X(τρεχοντας απο terminal και απο NetBeans)

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

class Animals:
population = 0
def __init__ (self,name,color,legs):
self.name = name
self.color = color
self. legs = legs
Animals.population +=1
print 'new animal created'

def introduce (self):
print 'Hi my name is '+ self.name

def __del__(self):
Animals.population -=1

@staticmethod
def howmany():
print 'There is %s animals left' %(Animals.population)

#create objects
Dog = Animals('jack','brown',4)
Cat = Animals('jen','white',4)

#how many animals
Animals.howmany()
#delete dog
del Dog
#remaing animals
Animals.howmany()

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

new animal created
new animal created
There is 2 animals left
There is 1 animals left
Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Animals.__del__ of <__main__.Animals instance at 0x7f1dfe3a5170>> ignored

αλλαζουμε τα
Κώδικας: Επιλογή όλων
Animals.population +=1
και
Κώδικας: Επιλογή όλων
Animals.population -=1

σε
Κώδικας: Επιλογή όλων
self.__class__.population +=1
και
Κώδικας: Επιλογή όλων
self.__class__.population -=1

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

class Animals:
population = 0
def __init__ (self,name,color,legs):
self.name = name
self.color = color
self. legs = legs
self.__class__.population +=1
print 'new animal created'

def introduce (self):
print 'Hi my name is '+ self.name

def __del__(self):
self.__class__.population -=1

@staticmethod
def howmany():
print 'There is %s animals left' %(Animals.population)

#create objects
Dog = Animals('jack','brown',4)
Cat = Animals('jen','white',4)

#how many
Animals.howmany()

del Dog

Animals.howmany()

που επιτελους εξαφανιζει το Exception AttributeError
στo προηγουμενo προγραμμα αυτο το None παραμενει αλλα δεν με πολυαπασχολει εφοσον ετρεξε ενα φυσιολογικα :roll: