如何在python中返回类函数?

2022-09-28 20:26:16 发布

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

我从第一个函数调用了另一个函数。 当我返回第一个函数时,屏幕上显示的是python None

class test():

    def test1(self, userinput):

        return self.test2(userinput)

    def test2(self, urls):

        string = "this is " + urls
        self.test3(string)

    def test3(self, sentence):
        return sentence


if __name__ == "__main__":

    objectt = test()
    seek = "concept of my functions"
    print(objectt.test1(seek))

Tags: 函数testselfstringreturndefseekurlssentence函数调用test1test2test3userinputobjectt
1条回答
网友
1楼 ·

test2中,调用test3

test3返回'this is concept of my function'到test2。然后,test2不向test1返回任何内容——因此,与所有不返回任何特定内容的函数一样,它返回None,然后由test1返回

必须在test2:class test()的末尾返回test3的输出:

class test:
    def test1(self, userinput):

        return self.test2(userinput)

    def test2(self, urls):

        string = "this is " + urls
        return self.test3(string)

    def test3(self, sentence):
        return sentence


objectt = test()
seek = "concept of my functions"
print(objectt.test1(seek))

# this is concept of my functions