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" becausename
isa class property
which is defined by__init__
. - When I run
a.__name__()
, it occurs an error because__name__
isa class property
,nota class function
Do all of my thoughts right ? Could someone explains to me ? And what the differences between__name__
andself.name
(in__init__
) ? Thanks a lot!