从另一个返回None的类函数调用Python类函数

时间:2011-06-08 01:15:41

标签: python class null

好的我有两节课:

PMBMessage和PPConsume

PPConsume实例化了PMBMessage,然后从PMBMessage调用一个函数来获取返回的字符串。以下是PPConsume的代码:

def handle(self, ch, delivery_tag, body):
    print "Got: " + body
    pmbmessage = PMBMessage(body)
    msg_format_id = pmbmessage.get_header_attribute("msg_format_id")
    print "back from the function"
    print msg_format_id
    if self.check_message(msg_format_id) == True:
        os.system('./send_job.sh ' + '\'' + body + '\'')
        print "success"
    else:
        print "fail"
def check_message(self, msg_format):
            #my error happens here
    if msg_format[0:3] != self.cfg.family:
        return False
    if msg_format[6:12] != self.cfg.index:
        return False
    return True

我评论了错误发生的位置,这里是PMBMessage类:

def get_header_attribute(self, attr):
    print "========================"
    print self.header
    print "========================"
    if self.header != {}:
        if attr in self.header:
            print str(self.header[attr])
            return str(self.header[attr])
        else:
            return False
    else:
        self.parse_header()
        print "HEY WE WENT THIS WAY"
        self.get_header_attribute(attr)

好的,这是这些函数运行时的输出 - 错误来自它返回None而不是你在实际得到的输出中看到的字符串,但是在一个类到另一个类之间它变为None:

Got:PMB01.00.0000THISISTHEFROMSYSTEM_THISISTHETOSYSTEM__STUFF________1111111111PP_IHI0000010100____messageuniqueid__messageuniqueid04/20/2011 12:0000:00:0000JOPP_IHI0000010100____messageuniqueid__messageuniqueid________________________________________/home/vcard/positivepay/meta/outbound/ppmbseops_7_vpa1_vpa1_clchk_20110527|simplecheck|path_register|2
========================
{}
========================
HEY WE WENT THIS WAY
========================
{u'to_system': 'THISISTHETOSYSTEM___', u'priority': '1111111111', u'original_unique_id': 'messageuniqueid__messageuniqueid', u'family': 'STUFF________', u'created': '04/20/2011 12:0000:00:0000', u'msg_format_id': 'PP_IHI0000010100____', u'hop_count': 'JO', u'msg_unique_id': 'messageuniqueid__messageuniqueid', u'message_data': '/home/stuff/stuff/stuff/outbound/filename|simple|path_register|2', u'padding': '________________________________________', u'original_msg_format_id': 'PP_IHI0000010100____', u'version': '01.00.0000', u'from_system': 'THISISTHEFROMSYSTEM_', u'message_type': 'PMB'}
========================
PP_IHI0000010100____
back from the function
None
Traceback (most recent call last):
  File "consume_message.py", line 4, in ?
    consume.consume()
  File "/home/khouser/Listener/PPCheckConsume.py", line 18, in consume
    channel.wait()
  File "/usr/lib/python2.4/site-packages/amqplib/client_0_8/abstract_channel.py", line 82, in wait
    return amqp_method(self, args, content)
  File "/usr/lib/python2.4/site-packages/amqplib/client_0_8/channel.py", line 1978, in _basic_deliver
    func(msg)
  File "/home/khouser/Listener/PPConsume.py", line 21, in handle_pyamqplib_delivery
    self.handle(msg.delivery_info["channel"], msg.delivery_info["delivery_tag"], msg.body)
  File "/home/khouser/Listener/PPConsume.py", line 29, in handle
    if self.check_message(msg_format_id) == True:
  File "/home/khouser/Listener/PPConsume.py", line 36, in check_message
    if msg_format[0:3] != self.cfg.family:
TypeError: unsubscriptable object

请告知,非常感谢。

1 个答案:

答案 0 :(得分:3)

递归调用需要再次返回其返回值,即

...
print "HEY WE WENT THIS WAY"
return self.get_header_attribute(attr)

否则简单地删除返回值,并且在递归调用返回后,控制到达函数的末尾并隐式返回None