- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个具有多个属性的可变对象(我们称之为 mutable
)。该对象本身是 Foo
类中的一个属性。 Foo
是用户定义的,而 mutable
不是,因此不能更改。
每当有人试图在 mutable
中设置属性时,我都需要进行计算。我面临的问题是,使用 mutable
的属性仅在设置了 mutable
本身而不是其属性时才有效。我已经设法解决了这个问题,但使用的东西似乎比合理的 Python 代码更像是 hack。
class Mutable(object): # Example class.
def __init__(self):
self.attr0 = 0
self.attr1 = 1
def __repr__(self):
return str(self.__dict__)[1:-1]
class Foo(object):
def __init__(self):
self._mutable = Mutable()
@property
def mutable(self):
print('mutable was read')
return self._mutable
@mutable.setter
def mutable(self, attr_value_pair):
attribute, value = attr_value_pair
setattr(self._mutable, attribute, value)
print('mutable.' + attribute, 'was set to', value)
bar = Foo()
print(bar.mutable) # 'mutable was read'
bar.mutable = ('attr0', 5) # 'mutable.attr0 was set to 5'
bar.mutable = ('attr1', 10) # 'mutable.attr1 was set to 10'
print(bar.mutable) # 'mutable was read'
# This is what I want to do but it only calls the getter.
bar.mutable.attr0 = 0 # 'mutable was read'
bar.mutable.attr1 = 1 # 'mutable was read'
有什么方法可以识别 mutable
中的属性何时以更 Pythonic 的方式设置?
编辑: 澄清一下:Foo
需要知道 mutable
何时更改,因为 Foo
具有依赖于可变
。 Mutable
可以被继承。
最佳答案
最简单干净的解决方案是子类化 mutable
- 但我认为这不是一个选项。
最简单的“quick&dirty”解决方案是使用 monkeypatch mutable
- 但这实际上是最后的解决方案,从长远来看是在寻找麻烦。
因此,如果您不能将 mutable
子类化,也不能控制它的实例化,并且不想对其进行 monkeypatch,那么您只能使用 proxy
模式。
编辑:哦,是的,因为它是 Foo
需要得到关于 self._mutable
更改的通知,你必须将它与观察者模式结合起来(这里是非常有限的形式):
class Mutable(object): # Example class.
def __init__(self):
self.attr0 = 0
self.attr1 = 1
def __repr__(self):
return str(self.__dict__)[1:-1]
class MutableProxy(object):
def __init__(self, mutable, owner):
self._mutable = mutable
self._owner = owner
@property
def attr0(self):
return self._mutable.attr0
@attr0.setter
def attr0(self, value):
self._mutable.attr0 = value
self._owner.notify("set", "attr0", value)
@property
def attr1(self):
return self._mutable.attr1
@attr1.setter
def attr1(self, value):
self._mutable.attr1 = value
self._owner.notify("attr1", value)
def __repr__(self):
return "<MutableProxy for {}>".format(repr(self._mutable))
class Foo(object):
def __init__(self):
self.mutable = Mutable()
@property
def mutable(self):
#print('mutable was read')
return self._mutable
@mutable.setter
def mutable(self, value):
self._mutable = MutableProxy(value, self)
def notify(self, attrname, value):
print('self._mutable.{} was set to {}'.format(attrname, value))
注意:我没有在 MutableProxy.__init__
中添加任何类型检查,这取决于你真正的 mutable
实际上是什么你可能想要确保你至少得到一些东西兼容...
NB2:我在 ProxyMutable
上使用了显式属性,因为它使事情更清晰,但您可能想使用 __getattr__
/__setattr__
Hook (至少对于您不需要控制访问的 mutable
属性)。
NB3:我们现在在 Foo
和 MutableProxy
之间有一个循环引用。 Python normalluy 知道如何摆脱循环引用,但如果它碰巧仍然是您的具体用例的问题,您可能希望将 MutableProxy._owner
设置为 weak reference相反。
现在困扰我的问题是:为什么要公开 mutable
?完全隐藏它并仅通过 Foo
方法或属性提供对其属性的访问将使代码更简单(并且更容易推理并且不太可能产生意外副作用)。
关于python - 如何识别属性的属性何时被设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41330968/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!