- 2

How would the attribute __a of the class b be accessed from outside the class?

How would the attribute __a of the class b be accessed from outside the class?

12th May 2020, 10:52 AM
Sri Vidhya
Sri Vidhya - avatar
5 Answers
+ 4
Like this- class b: __a = 5 def __init__(self): pass b1 = b() print(b1._b__a)
12th May 2020, 11:05 AM
Avinesh
Avinesh - avatar
+ 3
Thats cool. So Avinesh, you hide the class attribute with double underscore before and reveal it with single underscore before the call to the actual class after? And with this method, you'd have to see the source code to be able to do this?
12th May 2020, 11:11 AM
Slick
Slick - avatar
+ 3
Slick This one is straight from the course. Since I have started learning Python only for a week now, this one is fresh in mind. Python has no restrictions over the so called private members and can be easily accessed outside the class. The underscores are just like a warning to not access them but you can.
12th May 2020, 11:19 AM
Avinesh
Avinesh - avatar
+ 2
Avinesh very true, i remember reading something like that some time ago now. I came across some the other day and was having some trouble getting that. Thanks!
12th May 2020, 11:24 AM
Slick
Slick - avatar
0
_b__a
3rd Nov 2020, 1:08 PM
Haiyun Liu