1

demonstrates the way to hide the attributes of class and method to access the hidden variables outside the class

class hiding():
    # class attribute, "__" befor an attribute will make it to hide
    __hideAttr = 10
    def func(self):
        self.__hideAttr += 1 
        print(self.__hideAttr)

a = hiding()
a.func()
a.func()


#AttributeError: 'hiding' object has no attribute '__hideAttr'
print (a.__hideAttr)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Here_2_learn
  • 5,013
  • 15
  • 50
  • 68

1 Answers1

4

Accessing a hidden attribute results in error, comment out the below line to remove the error:

print (a.__hideAttr)

To access hidden attributes of a class, use:

print (a._hiding__hideAttr)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Here_2_learn
  • 5,013
  • 15
  • 50
  • 68