如果我将 python getter 和 setter 的值存储在 __dict__ 中会出现什么问题?
What will go wrong if I store the value for a python getter and setter in __dict__?
我正在使用 python getters 和 setters,但我不喜欢使用虚拟变量来存储值。例如,python 中的简单 getter 和 setter 可能定义如下:
class Foo(object):
def get_bar(self):
print('getting bar')
return self._bar
def set_bar(self,variable):
print('setting bar')
self._bar = variable
bar = property(get_bar,set_bar)
这使得 bar 像普通的日常属性一样工作,除了每次有人设置或读取它时它都会执行打印语句:
>>> my_fave_foo = Foo()
>>> my_fave_foo.bar = 5
setting bar
>>> print(my_fave_foo.bar)
getting bar
5
直到,也就是未来的我,决定用内省的方式,看看我最喜欢的Foo的属性:
>>> print(my_fave_foo.__dict__)
{'_bar': 5}
虽然我知道这不是什么大问题,但我还是很烦恼,所以我改为这样做 -
class Foo(object):
def get_bar(self):
print('getting bar')
return self.__dict__['bar']
def set_bar(self,variable):
print('setting bar')
self.__dict__['bar'] = variable
bar = property(get_bar,set_bar)
具有预期的行为
>>> my_fave_foo = Foo()
>>> my_fave_foo.bar = 5
setting bar
>>> my_fave_foo.bar
getting bar
5
>>> print(my_fave_foo.__dict__)
{'bar': 5}
我的问题是:为什么这是个坏主意?其他人,例如在回答这个问题时:
What's the Pythonic way to use Getters and Setters?
推荐下划线约定。我觉得我所做的有问题,但我不知道是什么。所以请告诉我,这会出什么问题?
我会很快注意到这是一个玩具示例,在我的实际代码中有使用 getters 和 setters 的真正原因。
I am using python getters and setters, but I don't like using a dummy variable to store the value.
为什么不呢?该值必须存在于某个地方,并且它在逻辑上是一个实例变量。 Python 没有 public/private.
Until, that is, future me decides to use introspection to look at the attributes of my favorite Foo:
那就不要那样做。 We are all responsible users.
so I [named the instance variable the same as the property]. why is this a bad idea?
现在您要依靠 属性 优先于字典项这一事实来理解这段代码。如果字典项和 属性 具有 不同的名称,那么内省的用户将很明显会调用某些特殊行为。
What will go wrong with this?
您的代码具有误导性,您下次查看时会感到困惑。
如果你愿意,你可以使用 self.__bar
作为内部状态,将名称改成 self._Foo__bar
,这是为了防止子类引起的冲突。参见 The Python Tutorial / Private Variables。
我正在使用 python getters 和 setters,但我不喜欢使用虚拟变量来存储值。例如,python 中的简单 getter 和 setter 可能定义如下:
class Foo(object):
def get_bar(self):
print('getting bar')
return self._bar
def set_bar(self,variable):
print('setting bar')
self._bar = variable
bar = property(get_bar,set_bar)
这使得 bar 像普通的日常属性一样工作,除了每次有人设置或读取它时它都会执行打印语句:
>>> my_fave_foo = Foo()
>>> my_fave_foo.bar = 5
setting bar
>>> print(my_fave_foo.bar)
getting bar
5
直到,也就是未来的我,决定用内省的方式,看看我最喜欢的Foo的属性:
>>> print(my_fave_foo.__dict__)
{'_bar': 5}
虽然我知道这不是什么大问题,但我还是很烦恼,所以我改为这样做 -
class Foo(object):
def get_bar(self):
print('getting bar')
return self.__dict__['bar']
def set_bar(self,variable):
print('setting bar')
self.__dict__['bar'] = variable
bar = property(get_bar,set_bar)
具有预期的行为
>>> my_fave_foo = Foo()
>>> my_fave_foo.bar = 5
setting bar
>>> my_fave_foo.bar
getting bar
5
>>> print(my_fave_foo.__dict__)
{'bar': 5}
我的问题是:为什么这是个坏主意?其他人,例如在回答这个问题时:
What's the Pythonic way to use Getters and Setters?
推荐下划线约定。我觉得我所做的有问题,但我不知道是什么。所以请告诉我,这会出什么问题?
我会很快注意到这是一个玩具示例,在我的实际代码中有使用 getters 和 setters 的真正原因。
I am using python getters and setters, but I don't like using a dummy variable to store the value.
为什么不呢?该值必须存在于某个地方,并且它在逻辑上是一个实例变量。 Python 没有 public/private.
Until, that is, future me decides to use introspection to look at the attributes of my favorite Foo:
那就不要那样做。 We are all responsible users.
so I [named the instance variable the same as the property]. why is this a bad idea?
现在您要依靠 属性 优先于字典项这一事实来理解这段代码。如果字典项和 属性 具有 不同的名称,那么内省的用户将很明显会调用某些特殊行为。
What will go wrong with this?
您的代码具有误导性,您下次查看时会感到困惑。
如果你愿意,你可以使用 self.__bar
作为内部状态,将名称改成 self._Foo__bar
,这是为了防止子类引起的冲突。参见 The Python Tutorial / Private Variables。