麻烦在python函数中返回变量

时间:2013-01-30 16:21:03

标签: python function return

我只是想修改一个字符串并返回修改后的字符串,但是,打印变量时我得到“None”。

def AddToListTwo(self,IndexPosition):
    filename = RemoveLeadingNums(self, str(self.listbox1.get(IndexPosition))) #get the filename, remove the leading numbers if there are any
    print filename #this prints None
    List2Contents = self.listbox2.get(0, END)
    if(filename not in List2Contents): #make sure the file isn't already in list 2
        self.listbox2.insert(0, filename)

def RemoveLeadingNums(self, words):
    if(isinstance(words,str)):
        match = re.search(r'^[0-9]*[.]',words)
        if match: #if there is a match, remove it, send it through to make sure there aren't repeating numbers
            RemoveLeadingNums(self, re.sub(r'^[0-9]*[.]',"",str(words)).lstrip())
        else:
            print words #this prints the value correctly
            return words
    if(isinstance(words,list)):
        print "list"

编辑 - 多人评论说如果匹配,我就不会返回值。如果有,我不想退货。它可以重复(例如:1.2.itema)。所以,我想基本上使用递归来删除它,然后返回值

2 个答案:

答案 0 :(得分:1)

RemoveLeadingNums返回None的条件有多种。例如如果采用if match:分支。也许那应该是:

if match: 
    return RemoveLeadingNums(...

如果您有任何非传入字符串的数据类型,也会返回None

答案 1 :(得分:1)

在比赛的情况下,你没有返回任何东西。它应该是:

return RemoveLeadingNums( ... )