替换函数调用的单词时,对象不可迭代

时间:2018-03-14 20:52:35

标签: python python-3.x basehttpserver

如何让otherImages返回其中的字符串,以便在从“narrow”方法调用时可以替换其中的单词?

    def otherImages(self):
        self.wfile.write(bytes("<div id='menu_button'><a href='/narrow'><img src='../images/menu_button.png'></a></div>", "utf8"))
                                                               #^word I want to replace
    def contentList(self, skip_name=''):  # All content methods in list 
        methods = [self.title, self.containerDIV, self.heading, self.stopSection, self.offlineSection, self.onlineSection, self.endDIV, self.otherImages]
        for m in methods:
            if m.__name__ != skip_name:
                m()

    def narrow(self):
        try:
            self.reply()
            self.contentList('onlineSection')   # removed onlineSection
            for words in self.otherImages():
                words.replace("narrow", "index")

2 个答案:

答案 0 :(得分:3)

self.otherImages没有return任何事情!当函数在python中没有返回显式值时,它返回None。您无法迭代None

答案 1 :(得分:0)

以下是我所做的改变,解决了我的问题。它允许我在从&#39; narrow&#39;中调用时编辑字符串。方法

def otherImages(self):
    return["<div id='menu_button'><a href='/narrow'><img src='../images/menu_button.png'></a></div>"]


def narrow(self):
    try:
        self.reply()
        self.contentList('onlineSection')   # removed onlineSectionv
        for words in self.otherImages():
            words = words.replace("/narrow", "/")
            self.wfile.write(bytes(words, "utf8"))
        return