0

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
  • 963,270
  • 265
  • 3,804
  • 3,187
Here_2_learn
  • 4,382
  • 9
  • 46
  • 67

1 Answers1

2

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
  • 107,083
  • 22
  • 201
  • 376
Here_2_learn
  • 4,382
  • 9
  • 46
  • 67