0

I wrote something in python3.6 using Pycharm as follows:

class Animal1:
    def __init__(self, name):
        self.__name = name


cat = Animal1("Tom")

class Animal2(Animal1):
    def __init__(self, name, sex):
        self.__sex = sex
        super(Animal2, self).__init__(name)
def toString(self):
    print("My name is {} and I'm {}".format(self.__name, self.__sex))


dog = Animal2("Jerry", "boy")

dog.toString()

And I just keep getting this weird warning:

Traceback (most recent call last):
  File "C:/Users/ZeiYeung/Desktop/my1thpy/main.py", line 30, in <module>
    dog.toString()
  File "C:/Users/ZeiYeung/Desktop/my1thpy/main.py", line 25, in toString
    print("My name is {} and I'm {}".format(self.__name, self.__sex))
AttributeError: 'Animal2' object has no attribute '_Animal2__name'

So I'm wondering if I was using the super() method in a wrong way?

Please check the code above and see what's going on...

:(

1s223
  • 11
  • 1
  • 2
    Don't use double-underscore names; they are meant to be *class private*, protecting them from accidental overwriting in a subclass. Subclasses are not *meant* to have easy access to such names. – Martijn Pieters Aug 18 '17 at 17:28
  • 1
    Why are you using double-leading-underscore names if you don't want what they do? *This is what double-leading-underscore names **do**.* – user2357112 Aug 18 '17 at 17:28

0 Answers0