python文件中的未绑定方法

时间:2012-09-28 07:45:24

标签: python

我试图找出为什么我的python代码给了我以下错误(我在这里阅读其他帖子,但我找不到问题)。错误如下:

"test_block/top_block.py", line 33, in __init__
    0, 0, 0, 0, 0, 1, 1, 1)
TypeError: unbound method make() must be called with my_block instance as first argument (got int instance instead)"

我的代码如下:

class top_block(grc_wxgui.top_block_gui):

    def __init__(self):
        grc_wxgui.top_block_gui.__init__(self, title="Top Block")
        _icon_path = "/usr/share/icons/hicolor/32x32/apps/gnuradio-grc.png"
        self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY))

        self.samp_rate = samp_rate = 32000

        self.extras_my_block_0 = gr_extras.my_block(2, 29, 10, 0, -0.1, 0.1, -0.01,
          0, 0, 0, 0, 0, 1, 1, 1)

        self.extras_message_dumper_0 = gr_extras.message_dumper()

        self.connect((self.extras_my_block_0, 0), (self.extras_message_dumper_0, 0))

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate

if __name__ == '__main__':
  parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
  (options, args) = parser.parse_args()
  tb = top_block()
  tb.Run(True)

感谢您的帮助,

1 个答案:

答案 0 :(得分:5)

此代码重现您的错误:

class gr_extras(object):
    def my_block(self, *args):
        return args

class top_block(object):
    def __init__(self):
        gr_extras.my_block(1, 2)

v = top_block()

执行时:

TypeError: unbound method my_block() must be called with gr_extras
    instance as first argument (got int instance instead)

您正在调用其他类中的方法而不进行实例化。这有效:

class top_block(object):
    def __init__(self):
        gr = gr_extras()
        gr.my_block(1, 2)