(Python) Using a Private attribute in a subclass

Asked

Viewed 22 times

-1

My wish is to validate my IF in class B using a private attribute that was done in class A. Nothing more than that, I am aware of some methods to verify an anonymous attribute but still I could not, any hints ? I brought this example but simple, the original question is much bigger , but managing to pass this problem I can fit in the biggest.

class a(object):
    def __init__(self):
         self.__atlas = 6


class b(a):
    def __init__(self):
         self.lol = 3


    def lol(self):
          super().__init__()
        
          if a.self.__atlas > 5:
              print("Hello")
        

 ob = b()
 ob.lol()
  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

  • There are some very curious things in this code of yours. How do you reset lol in the constructor of b, lol() cannot be invoked with a class instance. Invoking the parent-class constructor, in a method that is not the constructor of the daughter-class, is an unusual decision at least. In Python 3, every class inherits implicitly from object. Apart from that, about naming mangling, python has no private attributes, you can access the property atlas with self._a__atlas.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.