2017-08-10 2 views
0

Lorsque j'exécute ce code. Je reçois les erreurs suivantesLorsque j'exécute ce programme dans PyCharm, j'obtiens les erreurs suivantes

Traceback (most recent call last): File "C:/Users/Nabeel Hussain Syed/PycharmProjects/Hello World/check.py", line 80, in print(spot.toString()) File "C:/Users/Nabeel Hussain Syed/PycharmProjects/Hello World/check.py", line 66, in toString return "{} is {} cm tall and {} kilograms and say {}. His owner is {}".format(self.__name, AttributeError: 'Dog' object has no attribute '_Dog__name'

Open the link of the image to check out the errors.

class Animal: 
    __name = None 
    __height = 0 
    __weight = 0 
    __sound = 0 

    def __init__(self, name, height, weight, sound): 
     self.__name = name 
     self.__height = height 
     self.__weight = weight 
     self.__sound = sound 

    def set_name(self, name): 
     self.__name = name 

    def set_height(self, height): 
     self.__height = height 

    def set_weight(self, weight): 
     self.__weight = weight 

    def set_sound(self, sound): 
     self.__sound = sound 

    def get_name(self): 
     return self.__name 

    def get_height(self): 
     return str(self.__height) 

    def get_weight(self): 
     return str(self.__weight) 

    def get_sound(self): 
     return self.__sound 

    def get_type(self): 
     print("Animal") 

    def toString(self): 
     return "{} is {} cm tall and {} kilograms and say {}".format(self.__name, 
                  self.__height, 
                  self.__weight, 
                  self.__sound) 

cat = Animal('Whiskers', 33, 10, 'Meow') 
print(cat.toString()) 

class Dog(Animal): 
    __owner = "" 

    def __init__(self,name,height,weight,sound,owner): 
     self.__owner = owner 
     super(Dog,self).__init__(name,height,weight,sound) 

    def set_owner(self, owner): 
     self.__owner = owner 

    def get_owner(self): 
     return self.__owner 

    def get_type(self): 
     print("Dog") 

    def toString(self): 
     return "{} is {} cm tall and {} kilograms and say {}. His owner is {}".format(self.__name, 
                  self.__height, 
                  self.__weight, 
                  self.__sound, 
                  self.__owner) 


    def multiple_sounds(self, how_many=None): 
     if how_many is None: 
      print(self.get_sound()) 
     else: 
      print(self.get_sound() * how_many) 

spot = Dog("Spot", 53, 27, "Ruff", "Derek") 
print(spot.toString()) 
+2

S'il vous plaît, * s'il vous plaît *, ne pas utiliser cette "convention". Python n'est pas Java, ne le forcez pas. Utilisez les propriétés si vous devez contrôler l'affectation en dehors de la classe. – DeepSpace

Répondre

0

Attributs avec des noms commençant par doubles underscores sont considérés comme "privés", et non accessible depuis les classes d'enfants. Vous pouvez toujours y accéder par des noms comme _Animal__name (Animal est un nom de classe parent dans lequel l'attribut a été défini), mais c'est une mauvaise pratique.

Plus d'informations dans les documents officiels: https://docs.python.org/3.6/tutorial/classes.html#private-variables

0

la double-underscore a une signification en Python. S'il vous plaît voir cet extrait d'un previous stack overflow answer:

Double leading underscore

This one actually has syntactical significance. Referring to self.__var1 from within the scope of your class invokes name mangling. From outside your class, the variable will appear to be at self._YourClassName__var1 instead of self.__var1. Not everyone uses this - we don't at all where I work - and for simple classes it feels like a slightly absurd and irritating alternative to using a single leading underscore.

However, there is a justification for it existing; if you're using lots of inheritance, if you only use single leading underscores then you don't have a way of indicating to somebody reading your code the difference between 'private' and 'protected' variables - ones that aren't even meant to be accessed by subclasses, and ones that subclasses may access but that the outside world may not. Using a single trailing underscore to mean 'protected' and a double underscore to mean 'private' may therefore be a useful convention in this situation (and the name mangling will allow a subclasses to use a variable with the same name in their subclass without causing a collision).

+0

Merci beaucoup. Je comprends complètement maintenant. Je suivais une vidéo comme je suis un débutant à Python dans lequel le gars nommé les mêmes variables et il ne recevait pas d'erreurs. –