- 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?
5 Respostas
+ 4
Like this-
class b:
__a = 5
def __init__(self):
pass
b1 = b()
print(b1._b__a)
+ 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?
+ 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.
+ 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!
0
_b__a