0

呃,另一个编程问题,哈哈。

所以无论如何,我对使用 python 的 ctypes 非常感兴趣。ctypes 基本上允许您在 python 中调用 c 变量(真棒,我知道)所以这是您现在使用 ctypes 声明变量的方式:

import ctypes as c
class test(c.Structure):
   _fields_ = [
               ("example" , c.c_long),
               ...
              ]

但是,每当我使用字符串格式时,事情就是这样:

    print("test: %d" % (test.example)

它告诉我我需要它是 Python Integer,而不是 C long。

这就是它变得复杂的地方,因为没有真正声明示例,我不能做一个 .value 方法。它将返回语法错误。我不能将示例声明为 python 整数,因为没有办法做到这一点。(至少,据我所知)

任何帮助将不胜感激!

4

1 回答 1

4

test是一个类;test而是创建一个实例:

import ctypes as c

class test(c.Structure):
   _fields_ = [("example" , c.c_long)]

t = test(5)
print(t.example) # -> 5
print("%d" % t.example) # -> 5
于 2013-03-01T05:48:48.480 回答