Invoking return through a function?

bartc bc at freeuk.com
Sun Oct 29 16:01:00 EDT 2017


On 29/10/2017 17:02, Alberto Riva wrote:
> On 10/29/2017 12:21 PM, Rick Johnson wrote:

> In this specific case, I wanted to invoke a return if the key doesn't 
> exist (as I wrote in my post). But this is not strictly relevant, my 
> question was more general. To phrase it in yet another way: is there 
> *any other way* to invoke return on a function, except by explicitly 
> writing "return" in its body?

The proposal seems flawed.

Suppose function F() calls G(), and G invokes a double return, not only 
from G but from F too.

That suggests that G knows something about how it's going to be called, 
and knows that doing so isn't going to cause a problem because F was in 
the middle of something that needs to be completed before it returns.

In general, a function like G() could be invoked from 100 different 
call-sites all in different circumstances. Furthermore, those call-sites 
may be in code called from yet 100 other locations.

(It may also be necessary to consider what happens when G() isn't called 
from a function. Or G calls itself recursively. Or G() appears in a 
'return' expression. Or deep within a nested expression. Or when F() is 
itself called from within an expression, and is expected to have a 
certain return value.)

This may be why the other proposal I mentioned used it within nested 
functions so at least adding some constraints.

It is true that Python has exceptions that can do similar things, but 
the clue there is in the word 'exception'. It does seem that a lot of 
code uses exceptions which are routinely triggered.

-- 
bartc



More information about the Python-list mailing list