gpt4 book ai didi

传递给构造函数的 Python 函数引用变成 c_void_p 数据类型

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:28 25 4
gpt4 key购买 nike

长话短说,我试图将字典列表传递到容器类中,目的是每个字典都将用于实例化另一个类。问题是每个字典都包含一个要分配给子类的函数对象引用,并且由于某种原因,就在最里面的子类被实例化之前,它从 python 函数对象变成了 c_void_p 对象。

应用领域是使用 curses 创建基于文本的 UI 小部件库。

这是容器要包含的“子”类:

class DigitalReadout(Window):
# Just a one-line borderless window displaying some data...
def __init__(self, width, y, x, label, digits, data_source, parent=None):

super(DigitalReadout, self).__init__(1, width, y, x, parent)

self.data_source = data_source
self.data = self.get_data_from_source()
self.label = label
self.digits = digits
self.spaces = self.width - len(self.label) - self.digits # Calc Number of extra spaces

###Irrelevant display-related classes omitted###

def update_data(self):
self.data = self.get_data_from_source() #return data from function

def get_data_from_source(self):
return self.data_source.__call__()

这是“容器”类:

class ReadoutPanel(BoxedWindow):

def __init__(self, y, x, readouts, parent=None):

super(ReadoutPanel,self).__init__(2 + len(readouts), self.find_longest_readout_width(readouts) + 2, y, x, parent)
self.children = []
self.initialize_readouts(readouts)

def find_longest_readout_width(self, readouts):
#Find the longest member and size container accordingly
longest_length = 0
for each_dict in readouts:
this_dict_length = each_dict['digits'] + len(each_dict['label']) + 1
if this_dict_length > longest_length:
longest_length = this_dict_length
return longest_length

def initialize_readouts(self, readouts):
y_readout_index = 1
for each_hash in readouts:
function = each_dict['func']
function()
self.children.append(DigitalReadout(each_dict['digits'] + len(each_dict['label']) + 1,
1,
y_readout_index,
1,
function,
self.window))

作为引用,可以查看基类 Window 和 BoxedWindow here

当我运行以下测试代码时,出现后续错误:

if __name__ == '__main__':

#standard cuses initialization here...

from time import clock

i = 0

def print_i():
return str(i)

readouts = [{'label': 'count',
'digits': 10,
'func': print_i},
{'label': 'clock',
'digits':10,
'func': print_i}]

readout_panel = ReadoutPanel(1, 1, readouts) #Initialize that puppy!


curses.endwin()

错误:

 Traceback (most recent call last):
File "window.py", line 515, in <module>
readout_panel = ReadoutPanel(1, 1, readouts)
File "window.py", line 455, in __init__
self.initialize_readouts(readouts)
File "window.py", line 476, in initialize_readouts
self.window))
File "window.py", line 183, in __init__
self.data = self.data_source()
TypeError: 'c_void_p' object is not callable

Printlining 显示该函数正在从字典中获取并且仍然是一个函数对象。然而,一旦它被传递到 DigitalReadout 的构造函数中,它就会以某种方式返回一个 c_void_p 对象。知道为什么会这样吗?

提前致谢,对于这个冗长的问题深表歉意。

最佳答案

这是DigitalReadout的构造函数:

def __init__(self, width, y, x, label, digits, data_source, parent=None)

你是这样调用它的:

DigitalReadout(each_dict['digits'] + len(each_dict['label']) + 1, # width
1, # y
y_readout_index, # x
1, # label
function, # digits
self.window) # data_source

看起来您在构造函数中缺少一个参数 (height?),因为如果我没看错的话,该函数应该是 data_source 而它现在是数字

关于传递给构造函数的 Python 函数引用变成 c_void_p 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8979981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com