- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 win32com 从 Python 控制 Visio。
获取和设置形状表值非常简单:
print(shp.CellsU('PinX').ResultStr(''))
# and
shp.CellsU('PinX').FormulaU = '1'
到目前为止一切顺利,但我想要一个更短的语法,通过覆盖 setter 和 getter 来得到类似的东西:
print(shp.PinX)
# and
shp.PinX = '1'
所以我去找了一处特性:
ShapeClass = type(shp)
def SetPinX(self,value):
self.CellsU('PinX').FormulaU = value
def GetPinX(self):
return self.CellsU('PinX').ResultStr('')
ShapeClass.PinX = property(GetPinX,SetPinX)
现在奇怪的结果 - getter 工作正常(print(shp.PinX) 给出预期值),但 setter 不起作用。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\conda\conda\envs\YG_Package_1\lib\site-packages\win32com\client\__init__.py in __setattr__(self, attr, value)
478 try:
--> 479 args, defArgs=self._prop_map_put_[attr]
480 except KeyError:
KeyError: 'PinX'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-28-23f68b65624d> in <module>()
----> 1 shp.PinX= '1'
~\AppData\Local\conda\conda\envs\YG_Package_1\lib\site-packages\win32com\client\__init__.py in __setattr__(self, attr, value)
479 args, defArgs=self._prop_map_put_[attr]
480 except KeyError:
--> 481 raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
482 self._oleobj_.Invoke(*(args + (value,) + defArgs))
483 def _get_good_single_object_(self, obj, obUserName=None, resultCLSID=None):
AttributeError: '<win32com.gen_py.Microsoft Visio 15.0 Type Library.IVShape instance at 0x85710888>' object has no attribute 'PinX'
dir(ShapeClass) 很好地显示了 PinX 属性。
用自己的类进行测试也有效。因此,错误并不在于我实现该属性的方式。
我怀疑 win32com 存在 setter 被覆盖的问题。
有人知道如何解决这个问题吗?
最佳答案
win32com.client.DispatchBaseClass
基类使用 __setattr__
拦截所有属性设置访问。这会影响您的属性(property)对象;属性 setter 仅由默认的 object.__setattr__
实现调用,而不是由 win32com
使用的自定义方法调用。
所以,是的,shp.PinX = '1'
将调用DispatchBaseClass.__setattr__('PinX', '1')
,甚至如果类上定义了一个名为 PinX
的数据描述符,那么就会失败,因为它只支持 COM 接口(interface)定义的属性。
您必须在此处重写 __setattr__
方法以首先检查可用属性。您可以子类 DispatchBaseClass
或特定生成的类,或者我们可以直接对 win32com
进行猴子补丁:
import inspect
from win32com.client import DispatchBaseClass
dispatch_setattr = DispatchBaseClass.__setattr__
def allow_datadescriptor_setattr(self, name, value):
# for non-private names, check if the attribute exists on the class
# and is a data descriptor. If so, use object.__setattr__ to permit
# the data descriptor to intercept attribute setting
if name[:1] != '_' and inspect.isdatadescriptor(getattr(type(self), name, None)):
return object.__setattr__(self, name, value)
# private name, or doesn't exist on the class, or not a data descriptor
# invoke the original win32com dispatch __setattr__
dispatch_setattr(self, name, value)
DispatchBaseClass.__setattr__ = allow_datadescriptor_setattr
以上允许任何 descriptor with a __set__
or __delete__
method拦截对其名称的赋值,而不仅仅是 property
对象。
关于python - 通过 win32com 中的属性覆盖 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55431363/
因此,我需要获取在为其赋值时调用的 setter 的名称。像这样: var b = {}; var a = { set hey(value) { b[] = value; } } 我希望 se
我是 Android 编程的新手(~ 2 个月)有必要为几十个不同的变量设置 getter 吗? 例如—— //Yes I realise that this isn't 'dozens' publi
import Control.Lens import Control.Lens.TH data Foo = Foo { _bar, _baz :: Int } makeLenses ''
我有点困惑:我可以覆盖 setter/getter 但仍然使用 super setter/getter 吗?如果是 - 怎么办? 用例: class A { void set value(num
我有一个接收消息的应用程序。消息中存在可编辑的字段。当字段更改时,应将其保存到数据库中。不幸的是,setter 仅在 setter 的范围内更改给定字段的值。知道为什么会发生这种情况吗?这是 gett
C# 中有没有一种方法可以让 setter 从“某物”继承,这样每次为特定基类及其继承者调用 setter 时,我都可以运行一些代码? 我想做的是在我的基类上有一个名为 IsValid 的 bool
可能是一个我无法解决的非常简单的问题 - 我从 C# 开始,需要使用 getter/setter 方法向数组添加值,例如: public partial class Form1 : Form {
这两个属性实现有什么区别? public override string A { get { return "s"; } set { } } public override strin
是否可以使用 abc.abstractproperty 创建一个具体的 getter 但将 setter 抽象为每个继承类的不同。我为每个子类处理不同的 val 设置。 例如。 @abstractpr
我在某处看到类似下面的内容,想知道它是什么意思。我知道他们是getter和setter,但是想知道为什么字符串Type是这样定义的。谢谢你帮助我。 public string Type { get;
Public class Example { private int number; public Example(int number){ this.number =
假设我有这样的代码: public response MyMethod(Request req) { String id = req.getFirst().geId(); } 我已经模拟了主对
允许这样做: public int Age { get; set; } 但是应用程序是否为变量创建/分配空间?我经常这样做 private int age = 0; public int Age {
我有一个条件,我构造字符串 (finalValue) 的方式是基于我在输入中获得的非空值的数量。所以我想知道是否可以用一个不同的参数为字符串 (finalValue) 重载 setter 方法并根据我
例如,在这段代码中 var o = { set a(value) {this.b = value}, get a() {return this.b} } 是否有可能获得对 o.a 的 sett
我一直在努力了解 getter 和 setter,但没有深入了解。我读过 JavaScript Getters and Setters和 Defining Getters and Setters只是没
我想在我的类中添加一个 getter 和 setter。然而,setter 应该接收一个 querySelector,但 getter 返回一个新类型 pageSections。 我的问题是 gett
使用有什么好处: private var _someProp:String; public function set someProp(value:String):void { _somePr
当从域类调用它时,我想在我的 setter 中执行一些操作,而不是从 hibernate 中调用它时。此外,我正在使用 session 工厂,因此我无法使用 @PostLoad 来触发标志! 有人对此
人员类别: public class Person { private String firstName; private String lastName; public Pe
我是一名优秀的程序员,十分优秀!