class Foo(object):
def __init__(self, name):
self.__name = name
def index(self):
return self.__name
在终端中
>>> foo = Foo('chim')
>>> foo.index()
'chim'
>>> foo.__dict__
{'_Foo__name': 'chim'}
class Foo(object):
def __init__(self, name):
object.__setattr__(self, '_Foo__name', name)
def index(self):
# 这儿是双下划线
return self.__name
在终端中
>>> foo = Foo('chim')
>>> foo.index()
'chim'
>>> foo.__dict__
{'_Foo__name': 'chim'}