Try until it becomes an exception?

def foo():
    try:
        html = get(url)
        while (html is nogood):
            foo()
        return html
    except Exception as e:
        changeip()
        foo()

      

The function foo

just returns nice html in the text.
However, the function sometimes returns None

, and this is called when caught Exception

.

So, I changed the last line to return foo()

and it works as expected.

The question is why? In the case of catch, it just calls foo again, which ends up returning nice html in text, why do I need to add extra return

?

thank

+3


source to share


3 answers


You don't need a recursion solution at all. In foo

just continue the cycle until you get a good html. After each time in the loop, do something to improve your chance of getting good html (does it work changeip

for this purpose?).



def foo():
    while True:
        try:
            html = get(url)
            if is_good(html):
                return html
        except Exception:  # Need specific exception, do not catch all
            changeip()

      

+2


source


Think about what the first returns foo()

if it doesn't work.

Call foo1

from main

and it doesn't work -> foo1

calls foo2

and it succeeds -> foo2

returns html in -> foo1

other code to execute in foo1

so it returns None

.



When it foo

finally succeeds, it doesn't return html

in main

, it returns it to the last caller, which in this case foo1

.

The problem is you are discarding the results of any call foo

after the first failure. You need to chain the html back down the chain to the last line before return foo()

as you mentioned.

+1


source


Imagine a large cardboard box in the middle of the room. This block represents your function foo

. Now imagine that instead of return

what it means, we mean something like change_this_boxes_colour

. No matter what color the window changes (the return value, really) can be seen from the room you are currently in. This is how the function gives you information.

Now, the inside of this large cardboard box is a little bit magical because the box can create its own small boxes (including copies of itself!). So let's take a look at some code and then think about it in the field:

def bar():
    return BLUE  # every `bar` box turns `BLUE`

def foo():
    bar()

print(foo())  # prints None

      

Simple code? This is a super-distilled version of your code. We call a function inside another function, but we don't get its return value (color).

Thought in terms of boxes again, we have a large (colorless) box in the middle of the room. But he made his own box ( bar

) inside himself. This square turns into BLUE

as expected. But foo

it has no color set (return value), so it remains colorless.

We are still outside the large box foo

, so we cannot see what color the box is bar

. If we wanted to know which color is bar

rotated, foo

we would have to tell us. This can be achieved by asking to foo

set the same color as when viewing bar

go. In this case "BLUE"

.

def bar():
    return "BLUE"  # every `bar` box turns "BLUE"

def foo():
    return bar()  # change our colour to the same colour as `bar`

print(foo())  # prints "BLUE"

      

In your case, it foo

happens to create versions of itself inside your window, but the principle still applies.

+1


source







All Articles