473,378 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Shortcutting the function call stack

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
2 1621
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vinodh Kumar P | last post by:
What is call stack? I am sorry if its perceived as an off topic.
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
3
by: Shi Jin | last post by:
Hi there, I am now having a puzzle regarding what a function call actually does when called. I have a simple code to see what's going on. It is simply calling another simple function from...
6
by: Upendra | last post by:
Can any body explain me about the exact sequence of operations that takes place when a function call takes place. Consider all cases like functions with no return values and one or more retun...
11
by: Yahoo | last post by:
I have a programming issue where I need to know the whole history of the call stack to ensure it was never within a given method (specifically my own method). I am hooking into the XmlDocument...
3
by: Tommy Vercetti | last post by:
I have a complex threading deadlock scenario that I've been able to reproduce in the debugger. I hit break and look at the call stack which should tell me what I need. Except I only get the very...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
2
by: Zytan | last post by:
How can I show the function call stack window in VB 2005 Express? Debugging is rather hard without it... Thanks! Zytan
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.