简体   繁体   中英

Object is not iterable when replacing word called by function

How do I get otherImages to return the string in it so that I can replace a word within it when called from 'narrow' method?

    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 doesn't return anything! When a function does not return an explicit value in python, it returns None . You cannot iterate over None .

Here are the changes I made which solves my problem. It allows me to edit the string when called from the 'narrow' method.

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      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM