Question

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

but if return a line don't take 2 links..... like this :

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?

Was it helpful?

Solution

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):

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 1st example, you are printing the result. In the 2nd one, you are returning. The return statement is pretty much like a break, only that it returns a value too. Thus, when you return the first value, it breaks from the function.

OTHER TIPS

You can append them to a list and return them, like @aj8uppal's answer. You can also use the yield keyword.

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
  2. an almost identical function that does a return line on the first success thus terminating the entire sho_extra_dwn function. 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.


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()?

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

How many lines are printed? What is the return value of returner()?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top