473,289 Members | 1,917 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,289 software developers and data experts.

How to add function return value

I need to write functions that return locals() as follows,
def func1():
a = 1
return locals()

def func2():
b = 2
return locals()

Can I write a decorator that it can automately do this conversion

def func1()
a = 1

--->

def func1():
a = 1
return locals()

Jun 27 '08 #1
2 1244
On May 30, 6:21*pm, HYRY <ruoyu0...@gmail.comwrote:
Can I write a decorator that it can automately do this conversion

def func1()
* * a = 1

--->

def func1():
* * a = 1
* * return locals()
Not sure why you would want to do this, but there are several ways.

1. Make bytecode hack decorator that transforms the final "return
None" into "return locals()". A recipe that shows the basic technique
is at: http://aspn.activestate.com/ASPN/Coo.../Recipe/277940

2. Retrieve the source using inspect.getsourcelines(f). Then, append a
"return locals()" to the end of the function and run it through exec.

3. Try hacking a tracing/debugging utility.

4. Run the sourcefile through tokenize, make the appropriate
insertion, and then untokenize.

. . .

Raymond
Jun 27 '08 #2
On May 30, 10:16*pm, Raymond Hettinger <pyt...@rcn.comwrote:
On May 30, 6:21*pm, HYRY <ruoyu0...@gmail.comwrote:
Can I write a decorator that it can automately do this conversion
def func1()
* * a = 1
--->
def func1():
* * a = 1
* * return locals()

Not sure why you would want to do this, but there are several ways.

1. Make bytecode hack decorator that transforms the final "return
None" into "return locals()". *A recipe that shows the basic technique
is at:http://aspn.activestate.com/ASPN/Coo.../Recipe/277940

2. Retrieve the source using inspect.getsourcelines(f). Then, append a
"return locals()" to the end of the function and run it through exec.

3. Try hacking a tracing/debugging utility.

4. Run the sourcefile through tokenize, make the appropriate
insertion, and then untokenize.

Here's an illustration of (3):

import sys
import functools

def withlocals(f):
@functools.wraps(f)
def wrapper(*args, **kwds):
f_locals = {}
def probe(frame, event, arg):
if event == 'return':
f_locals.update(frame.f_locals)
return probe
sys.settrace(probe)
try: res = f(*args,**kwds)
finally: sys.settrace(None)
return (res, f_locals)
return wrapper

# example

@withlocals
def foo(x, y=0, *args, **kwds):
a = max(x,y)
b = len(args)
c = min(kwds.values())
return a+b+c

r,locs = foo(1,2,3,4,a=5,b=6)
print locs

George
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
4
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
8
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.