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

Object is not iterable when replacing word called by function

如何将 otherImages 获取到 return 其中的字符串,以便在从 '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")

self.otherImages 没有 return 任何东西!当一个函数没有 return python 中的显式值时,它 return 是 None。您不能遍历 None.

以下是我所做的更改,解决了我的问题。它允许我在从 'narrow' 方法调用时编辑字符串。

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