Python方法解析之谜
我不明白为什么这个程序失败了。
#!/usr/bin/env python
from __future__ import division, print_function
from future_builtins import *
import types
import libui as ui
from PyQt4 import QtCore
import sip
p = ui.QPoint()
q = QtCore.QPoint()
def _q_getattr(self, attr):
print("get %s" % attr)
value = getattr(sip.wrapinstance(self.myself(), QtCore.QPoint), attr)
print("get2 %s returned %s" % (attr, value))
return value
p.__getattr__ = types.MethodType(_q_getattr, p)
print(p.__getattr__('x')()) # Works! Prints "0"
print(p.x()) # AttributeError: 'QPoint' object has no attribute 'x'
我使用 Boost.Python 创建 libui,它公开了 QPoint 类。我还包含了 PyQt4,它有一个 sip 暴露的 QPoint。我正在尝试完成两种类型之间的映射。
我检查了 p
是一个新式类,那么为什么 __getattr__
没有被 px()
调用呢?
I can't figure out why this program is failing.
#!/usr/bin/env python
from __future__ import division, print_function
from future_builtins import *
import types
import libui as ui
from PyQt4 import QtCore
import sip
p = ui.QPoint()
q = QtCore.QPoint()
def _q_getattr(self, attr):
print("get %s" % attr)
value = getattr(sip.wrapinstance(self.myself(), QtCore.QPoint), attr)
print("get2 %s returned %s" % (attr, value))
return value
p.__getattr__ = types.MethodType(_q_getattr, p)
print(p.__getattr__('x')()) # Works! Prints "0"
print(p.x()) # AttributeError: 'QPoint' object has no attribute 'x'
I used Boost.Python to create libui, which exposes the class QPoint. I aso included PyQt4, which has a sip-exposed QPoint. I'm trying to accomplish a mapping between the two types.
I checked that p
is a new-style class, so why isn't __getattr__
being called for p.x()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这与其他人遇到的问题有些相似 就在昨天。简而言之,似乎特殊方法(如 __getattr__、
__str__
、__repr__
、__call__
等)在新式类实例中不可重写,即只能在其类型中定义它们。这是我针对该问题的解决方案的改编,希望对您有用:
This is somewhat similar to the issue someone else has encountered just yesterday. In short, it seems like special methods (like
__getattr__
,__str__
,__repr__
,__call__
and so on) aren't overridable in new-style class instance, i.e. you can only define them in its type.And here's an adaptation of my solution for that problem which should hopefully work for yours:
我建议你不要尝试在 boost python 中公开 QPoint。您应该能够使用 boost 注册与 python 之间的转换器,该转换器将使用 SIP api 函数将 QPoint 与 python 之间转换为 sip 对象。
我已经做到了,但最近还不够提供更多细节。
I suggest that you not attempt to expose QPoint in boost python. You should be able to register converters to/from python with boost that will use the SIP api functions to convert QPoint from/to python as the sip objects.
I've done it, but not recently enough to give more details.
这是一个如何集成 PyQt4 和 boost::python 的示例,
首先我们必须定义包装/解开函数来处理裸指针
,然后我们必须注册我们想要集成的所有类
,例如我们有一个可以使用的类。 QLabel:
我们也必须将此类公开给 python:
现在我们准备好进行交互,
在 C++ 大小上做类似这样的事情
python:
在其他情况下,如果你不想将自己的 Q 对象发送到 PyQt4 :
python:
这是我真正的小帮手:
This is an example how to integrate PyQt4 and boost::python
first of all we must define wrap/unwrap function to deal with bare pointers
after that we must register all classes we want integrate to
and for example we have class that works with.. QLabel:
we must expose this class to python too:
now we a ready to interaction,
on C++-size do something like this
python:
In other case if you wan't send you own Q-object to PyQt4 :
python:
And this is my real little helper: