473,788 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1637
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)==Tr ue", or even
"if ((arg==True)==T rue)==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_nece ssary:
hijack_caller_f unction_and_mak e_it_return(1)

def my_function1(ar g):
hijacker(someth ing)
... # Continue as normal
return 2

def my_function2(ar g):
... # Maybe do some processing here
hijacker(whatev er)
... # Continue as normal
return 3
You could simply do something like:

def hijacker(arg):
if I_feel_its_nece ssary:
return True, 1
else:
return False, 0

def my_function1(ar g):
abort, code = hijiacker(somet hing);
if abort: return code
... # Continue as normal
return 2

def my_function2(ar g):
... # Maybe do some processing here
abort, code = hijiacker(whate ver);
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
2370
by: Vinodh Kumar P | last post by:
What is call stack? I am sorry if its perceived as an off topic.
39
6555
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. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
3
2813
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 main(). The disassembled code for the function under linux (with gcc) looks like this: pushl %ebp ;push old %ebp movl %esp, %ebp ;make new stack frame
6
1708
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 values(directly or indirectly) and functions with different types of formal arguments passed.
11
3900
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 nodechanged event, I need to from this event call modify a different node in this xmldocument, thus without somekind of check we will be off in an infinite loop. Currently I am using the System.Diagnostics classes but feel its not right to use these...
3
1538
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 bottom of the call stack: KERNEL32.DLL!7c573b28() KERNEL32.DLL!7c573b50() boost_thread-vc71-mt-gd-1_31d.dll!boost::detail::condition_impl::do_wait() Line 198 + 0xf C++ Sniper.exe!boost::condition::do_wait<boost::mutex>() Line 150 + 0xd C++
24
6590
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 the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If this is the case, the address should go upwards (increasing) or downards (decreasing) then? i.e....
2
8369
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
3110
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{
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.