在python中通过引用传递整数 [英] Passing integer by reference in python

查看:142
本文介绍了在python中通过引用传递整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PyQt与Python中的ActiveX COM对象对话.此方法在Python中有效:

I'm attempting to use PyQt to talk to an ActiveX COM object in Python. This method works in Python:

myobj.dynamicCall('SetAbsMovePos(int, double)', [0, 10])

但是,我不能通过引用发送参数.从文档中,我想调用的方法是:

However, I can't send arguments by reference. From the docs, the method I wish to call is:

int GetNumHWUnits (int lHWType, int& plNumUnits)

,基本示例代码为:

QVariantList params = ...
int result = object->dynamicCall("GetNumHWUnits(int, int&)", params).toInt();

我假定使用ctypes的Python中的等效代码应为:

I presume the equivalent code in Python, using ctypes, should be:

num = ctypes.c_int()
myobj.dynamicCall('GetNumHWUnits(int, int&)', [6, ctypes.byref(num)])

num从未更新.

Python中等效的代码或解决方法是什么,如何使用Python发送并稍后读取类型为int&的参数?

What's the equivalent code or workaround in Python, and how can I send and later read an argument of type int& using Python?

推荐答案

从PyQt邮件列表中获取解决方案.需要引用参数列表,因为只有该列表会被更新;原始的QVariant对象永远不会.回想起来似乎很明显!

Got the solution from the PyQt mailing list. A reference to the list of arguments is needed, as only that list is updated; the original QVariant objects never are. Seems fairly obvious in retrospect!

typ = QVariant(6)
num = QVariant(0)
args_list = [typ, num]

self.dynamicCall('GetNumHWUnits(int, int&)', args_list)

print 'Num of HW units =', args_list[1].toInt()[0]

完整的代码示例显示在这篇文章中.

A full code example is shown in this post.

这篇关于在python中通过引用传递整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆