While learning Python, I came accross this strange behavior :
Consider this simple class :
class Human(object):
def __init__(self, name):
self.__name = name
def description(self):
print("Hello, my name is %s" % self.__name)
I want to avoid being able to alter the name after the object has been created.
If I use this :
MyHuman = Human("Andy")
MyHuman.__name = "Arthur"
MyHuman.age = 23
MyHuman.description()
print MyHuman.age
It doesn't change the name after the object instanciation, which is good because I want the instance variable to be private. On the other hand, I thought it would complain about accessing this variable. It doesn't even complain about accessing a mysterious "age" variable and prints it properly later.
I come from C# and it seems odd to me. Where is my mistake ?
You should know the following:
"_<className>__<attributeName>"
With that name, it can be accessed from outside. When accessed from inside the class, the name will be automatically changed correctly.
To use emulate readonly/private variables in Python, use property
syntax. (Comments/other answers point out the variable can still be accessed if you know how--thanks for the heads up). Here is one way to do it:
>>> class Test:
def __init__(self):
self.__a = 1
@property
def a(self):
return self.__a
>>> t = Test()
>>> t.a
1
>>> t.a = 2
AttributeError: cannot set attribute
>>> t.__a
AttributeError: 'Test' object has no attribute '__a'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With