替换由函数调用的单词时,对象不可iterable

2024-10-02 08:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    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")

Tags: nameinselfdivfordefbuttonreplace
2条回答

以下是我所做的改变,解决了我的问题。它允许我在从“窄”方法调用时编辑字符串。你知道吗

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      

self.otherImages没有return任何东西!当函数在python中不返回显式值时,它返回None。不能在None上迭代。你知道吗

相关问题 更多 >

    热门问题