No return to the parent function

J

Joan Miller

I've a main function called i.e. *foo()* which has a block of code
that is repetead several times (for the error catching code and error
reporting), but that code has a return to exit of *foo()*

-----------
foo():
...
if self.background:
_log.exception(str(error))
return ReturnCode.ERROR, None
else:
raise NameError(error)
 
A

Arnaud Delobelle

Joan Miller said:
I've a main function called i.e. *foo()* which has a block of code
that is repetead several times (for the error catching code and error
reporting), but that code has a return to exit of *foo()*

-----------
foo():
...
if self.background:
_log.exception(str(error))
return ReturnCode.ERROR, None
else:
raise NameError(error)

-----------

So I would tu put that code block into a separated function (*throw()
*), but the problem is that it returns to the parent function (foo()).
How to solve it?

If I understand correctly, you can simply do this:

def throw(...):
if ...:
...
return ...
else:
raise ...

def foo():
...
return throw(...)

HTH

I.e. if throw returns something, foo returns it as well. If throw
raises an exception, it will go through foo. Is this what you want?
 
J

Joan Miller

If I understand correctly, you can simply do this:

def throw(...):
    if ...:
        ...
        return ...
    else:
        raise ...

def foo():
    ...
    return throw(...)

HTH

I.e. if throw returns something, foo returns it as well.  If throw
raises an exception, it will go through foo.  Is this what you want?

Yes, that is it. It was more simple that I had thinked, thanks!
 
M

MRAB

Joan said:
I've a main function called i.e. *foo()* which has a block of code
that is repetead several times (for the error catching code and error
reporting), but that code has a return to exit of *foo()*

-----------
foo():
...
if self.background:
_log.exception(str(error))
return ReturnCode.ERROR, None
else:
raise NameError(error)

-----------

So I would tu put that code block into a separated function (*throw()
*), but the problem is that it returns to the parent function (foo()).
How to solve it?

Call it in a return statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top