Ho il seguente codice, che ha i seguenti due problemi:metodo principale pitone
Traceback (most recent call last):
File "C:\Users\v\workspace\first\src\tests.py", line 1, in <module>
class Animal:
File "C:\Users\v\workspace\first\src\tests.py", line 39, in Animal
File "C:\Users\v\workspace\first\src\tests.py", line 31, in main
dog = Animal()
NameError: global name 'Animal' is not defined
Questo codice è da un tutorial, e nel tutorial funziona bene. Ho il Python 2.7 e uso il plugin PyDev per Eclipse.
class Animal:
__hungry = "yes"
__name = "no name"
__owner = "no owner"
def __init__(self):
pass
def set_owner(self,newOwner):
self.__owner= newOwner
return
def get_owner(self):
return self.__owner
def set_name(self,newName):
self.__name= newName
return
def get_name(self):
return self.__name
def noise(self):
print('errr')
return
def __hiddenmethod(self):
print("hard to find")
def main():
dog = Animal()
dog.set_owner('Sue')
print dog.get_owner()
dog.noise()
if __name__ =='__main__':main()
Potete fornire un collegamento a questo tutorial? Chiunque lo abbia scritto dovrebbe essere fucilato, o almeno bandito dalla scrittura. Questo * non * come scrivi proprietà di classe in Python. –
Ascolta Daniel e cerca il decoratore '@ property' in Python. Non devi scrivere getter e setter del genere. – katrielalex
Inoltre, non usare nomi a doppio trattino - sono lì per un caso d'uso molto particolare. Se si desidera indicare che una proprietà è "solo per uso interno", utilizzare invece single-underscoring. – katrielalex