Privet variable in class in Python

Viewed 14

I don't know why but my code steel giving me an error

I think python can't access the __name and __age variable Because it is inherited from the class World

is anyone have an idea about how I can access it in my class Cat?

class World(object):
    def __init__(self, age):
        self.__age = age
        self.__name = None

    def get_age(self):
        return self.__age

    def get_name(self):
        return self.__name

    def set_age(self, newAge):
        self.__age = newAge

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

    def __str__(self):
        return f"object: {self.__name} : {self.__age}"

################
################

class Cat(World):
    def __init__(self, age):
        super().__init__(age)

    def speak(self):
        print("meow")

    def __str__(self):
        return f"Cat:{self.__name} : {self.__age}"

################
################

cat1 = Cat(25)
print(cat1.get_age())
print(cat1.set_age(23))
print(cat1.get_age())
cat1.set_name("Fake name")
print(cat1)
cat1.speak()

Error

thanks...

0 Answers
Related