By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Shortcutting the function call stack

P: n/a
Hello all,

I would like to do something like:

def called(arg)
if arg==True:
!!magic!!caller.return 1

def caller(arg)
called(arg)
return 2

Here, the fake !!!magic!!! represents a statement (which I ignore)
that would make the caller function return a value different from what
it'd return normally.

For example, caller(True) would return 1, and caller(False) would
return 2. The reason I want that is because I don't want the caller
function to know what's going on in the called function, and be
shortcut if the called function think it's necessary.

Would you know if that's possible, and if so, how?

I've done a bit of research and I think I've found some good pointers,
in particular using the 'inspect' library:

import inspect

def called(arg)
if arg==True:
caller_frame = inspect.stack()[1]
...

Here 'caller_frame' contains the frame of the caller function. Now,
how can I make that frame return a particular value?

By the way, I'm not really interested in 'called' throwing an
exception and 'caller' catching it. In fact, I want things to remain
completely transparent for 'caller'.

Hope that was clear... :/

Thanks!

Julien
Mar 24 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 24 Mar 2008 04:21:29 -0700, Julien wrote:
Hello all,

I would like to do something like:

def called(arg)
if arg==True:
!!magic!!caller.return 1
Instead of writing "if arg==True", or "if (arg==True)==True", or even
"if ((arg==True)==True)==True", you should just write:

"if arg:"

That's probably all you need.

>
def caller(arg)
called(arg)
return 2

def called(arg):
return 1

def caller(arg):
if arg: return called(arg)
return 2
And if you wish to change the place where the decision is made:

def called(arg):
if arg: return 1
else: return 2

def caller(arg):
return called(arg)

Here, the fake !!!magic!!! represents a statement (which I ignore) that
would make the caller function return a value different from what it'd
return normally.
The statement you use to return a value different from what you would
normally return is the "return" statement. You need to call that
statement from the function doing the returning, not from another
function.
The reason I want that is because I don't want the caller function to
know what's going on in the called function, and be shortcut if the
called function think it's necessary.
Not knowing what's going on in called functions is why functions were
invented in the first place. That's what they do.

What shortcut do you think you might want to take? There are probably
better solutions than playing around with the internals of the
interpreter. I have a feeling you are trying to implement some sort of
function cache, maybe...

def check_in_cache(arg): # fake cache code
if arg:
!!magic!!caller.return 1 # not real Python

def function(arg):
check_in_cache(arg) # magic happens here
# but if it doesn't, we do lots of calculations here
return 2 # and finally return
Is that the sort of thing you're trying for? If so, that's not the way to
go about it.

--
Steven
Mar 24 '08 #2

P: n/a
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
Mar 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.