Shortcutting the function call stack
Francesco Bochicchio
bockman at virgilio.it
Tue Mar 25 03:49:31 EDT 2008
Il Mon, 24 Mar 2008 15:05:38 -0700, Julien ha scritto:
...
>
> I'll try to explain a bit more what I'm after, and hopefully that will
> be clearer. In fact, what I'm trying to do is to "hijack" (I'm making up
> the term) a function:
>
> def hijacker(arg):
> if I_feel_its_necessary:
> hijack_caller_function_and_make_it_return(1)
>
> def my_function1(arg):
> hijacker(something)
> ... # Continue as normal
> return 2
>
> def my_function2(arg):
> ... # Maybe do some processing here
> hijacker(whatever)
> ... # Continue as normal
> return 3
>
>
>
You could simply do something like:
def hijacker(arg):
if I_feel_its_necessary:
return True, 1
else:
return False, 0
def my_function1(arg):
abort, code = hijiacker(something);
if abort: return code
... # Continue as normal
return 2
def my_function2(arg):
... # Maybe do some processing here
abort, code = hijiacker(whatever);
if abort: return code
... # Continue as normal
return 3
Although purists may frown upon the double return statement, it is still
a much cleaner code than using some magic to abort the caller function.
And tou have only to add two lines - always the same - at each function.
Ciao
-----
FB
More information about the Python-list
mailing list