class Test(object):
def __init__(self):
self.name = "changzhi"
@property
def __name__(self):
return self.name
>>> a = Test()
>>> a.__name__
>>> "changzhi"
>>> a.name
>>> "changzhi"
>>> a.__name__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Here are my thoughts:
- When I run
a.__name__, it returns "changzhi" because__name__is a property ofclass Test(object)which is modified by "@property". - When I run
a.name, it returns "changzhi" becausenameisa class propertywhich is defined by__init__. - When I run
a.__name__(), it occurs an error because__name__isa class property,nota class functionDo all of my thoughts right ? Could someone explains to me ? And what the differences between__name__andself.name(in__init__) ? Thanks a lot!