简体   繁体   English

函数中的print和return之间有什么区别?

[英]What is the difference between print and return in a function?

I have this function : 我有这个功能:

def sbo_extra_dwn(link, name):
    sbo_url_dwn = link + name + ".info"
    sbo_url_dwn = sbo_url_dwn.replace("repository", "slackbuilds")

    f = urllib2.urlopen(sbo_url_dwn)
    read_page = f.read()
    for line in read_page.splitlines():
            if line.startswith(' '):
                    line = line[10:-1]
            if line.startswith('http'):
                    print line 

This prints two links : http://downloads.sourceforge.net/project/unvanquished/Assets/unvanquished_0.26.0.pk3 http://sourceforge.net/projects/unvanquished/files/Assets/unvanquished_0.25.0.pk3 这将打印两个链接: http : //downloads.sourceforge.net/project/unvanquished/Assets/unvanquished_0.26.0.pk3 http://sourceforge.net/projects/unvanquished/files/Assets/unvanquished_0.25.0.pk3

but if return a line don't take 2 links..... like this : 但是,如果返回一行,则不要使用2个链接.....像这样:

def sbo_extra_dwn(link, name):
    sbo_url_dwn = link + name + ".info"
    sbo_url_dwn = sbo_url_dwn.replace("repository", "slackbuilds")

    f = urllib2.urlopen(sbo_url_dwn)
    read_page = f.read()
    for line in read_page.splitlines():
            if line.startswith(' '):
                    line = line[10:-1]
            if line.startswith('http'):
                    return line

the result is only one link: 结果只有一个链接:

How can return two more if necessary? 如有必要,又怎么能退还两个?

Append your results to a list, and then return that list. 将结果追加到列表中,然后返回该列表。

def sbo_extra_dwn(link, name):
    results = []
    sbo_url_dwn = link + name + ".info"
    sbo_url_dwn = sbo_url_dwn.replace("repository", "slackbuilds")

    f = urllib2.urlopen(sbo_url_dwn)
    read_page = f.read()
    for line in read_page.splitlines():
            if line.startswith(' '):
                    line = line[10:-1]
            if line.startswith('http'):
                    results.append(line)
    return results

However, you might want to return a tuple because of their immutability (suggested by @Moe ): 但是,由于它们的不可变性(由@Moe建议),您可能要返回一个元组:

def sbo_extra_dwn(link, name):
    results = ()
    sbo_url_dwn = link + name + ".info"
    sbo_url_dwn = sbo_url_dwn.replace("repository", "slackbuilds")

    f = urllib2.urlopen(sbo_url_dwn)
    read_page = f.read()
    for line in read_page.splitlines():
            if line.startswith(' '):
                    line = line[10:-1]
            if line.startswith('http'):
                    results = results+(line,)
    return results

In your 1 st example, you are print ing the result. 第一个示例中,您正在print结果。 In the 2 nd one, you are return ing. 第二个中,您正在return The return statement is pretty much like a break , only that it returns a value too. return语句非常类似于break ,只是它也返回一个值。 Thus, when you return the first value, it breaks from the function. 因此,当您返回第一个值时,它会从函数中中断。

You can append them to a list and return them, like @aj8uppal's answer. 您可以将它们附加到列表中并返回它们,例如@ aj8uppal的答案。 You can also use the yield keyword. 您也可以使用yield关键字。

def sbo_extra_dwn(link, name):
    sbo_url_dwn = link + name + ".info"
    sbo_url_dwn = sbo_url_dwn.replace("repository", "slackbuilds")

    f = urllib2.urlopen(sbo_url_dwn)
    read_page = f.read()
    for line in read_page.splitlines():
            if line.startswith(' '):
                    line = line[10:-1]
            if line.startswith('http'):
                    yield line

And then, you can use it like this: 然后,您可以像这样使用它:

lines = sbo_extra_dwn(my_link, my_name)
for l in lines:
    ....

Hope this helps! 希望这可以帮助!

Your two code samples have two wildly different behaviors: 您的两个代码示例具有两种截然不同的行为:

  1. print which shows something on the standard output and allows the for line loop to continue and print other matches but returns None when execution falls off the end of sbo_extra_dwn print在标准输出上显示某些内容,并允许for line循环继续进行并打印其他匹配项,但是当执行落在sbo_extra_dwn的末尾时返回None
  2. an almost identical function that does a return line on the first success thus terminating the entire sho_extra_dwn function. 一个几乎相同的函数,在第一次成功时执行return line ,从而终止整个sho_extra_dwn函数。 This does return a value, but only the first one found that matches the criteria. 确实返回一个值,但仅找到第一个与条件匹配的值。

Other answers here "gave you a fish" instead of showing you the mistake you made in your fishing technique. 这里的其他答案是“给你一条鱼”,而不是向您显示您在捕鱼技术上所犯的错误。 You need to understand the important difference between print and return and then the fix will make sense. 您需要了解printreturn之间的重要区别,然后进行修复。


Compare these two bits of minimal code and how their behavior differs: 比较这两个最小代码及其行为的不同之处:

def printer():
    for c in 'user3634982':
        print c

How many lines are printed? 打印多少行? What is the return value of printer() ? printer()的返回值是多少?

def returner():
    for c in 'user3634982':
        return c

How many lines are printed? 打印多少行? What is the return value of returner() ? returner()的返回值是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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