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

总而言之,我试图将一个字典列表传递给一个容器类,意图是每个字典将用于实例化另一个类.问题是每个字典包含一个要分配给子类的函数对象引用,并且由于某种原因,在最内层子类被实例化之前,它从python函数对象变为c_void_p对象.

应用程序域是使用curses创建基于文本的UI小部件库.

这是容器要包含的’child’类:

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

看起来你在构造函数中缺少一个参数(高?),因为如果我正确读取它,该函数应该是data_source,它现在是数字.

上一篇:python – 使用(n)curses打印到终端的右侧或底部


下一篇:Web APP开发技巧总结(转)