簡體   English   中英

替換函數調用的單詞時對象不可迭代

[英]Object is not iterable when replacing word called by function

我如何獲取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")

self.otherImagesreturn任何東西! 當函數未在python中返回顯式值時,它將返回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      

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM