Search code examples
pythonoopmethodsprivateencapsulation

Confusion over output of encapsulation method code in Python


    class encap:
          __a = 10
          b=11
          def __abc(self):
              print(self.b)
              print(self.__a)
          def xyz (self):
              # calling private method 
              self.__abc()

    a1= encap()
    print(a1.xyz())

Here __a and __abc are private. So I am calling __abc() in xyz() method. Getting the output as

11
10
None

I understand about getting 11 and 10 but why should I got None as well ?


Solution

  • You are printing the result of a1.xyz(), which returns None (because there is no return statement in encap.xyz). Simply calling a1.xyz() will be enough to print what you expect.