0
    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 ?

2 Answers2

0

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.

michaeldel
  • 2,204
  • 1
  • 13
  • 19
0

By default, a method in Python returns None if you return nothing from it.

You can validate the same from the below code:

def test():
    print('Hello, world!')

print(test())

Output:

Hello, world!
None
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35