Re: How to add function return value



On May 30, 6:21 pm, HYRY <ruoyu0...@xxxxxxxxx> wrote:
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/Cookbook/Python/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
.



Relevant Pages

  • Re: How to add function return value
    ...     return locals ... Make bytecode hack decorator that transforms the final "return ... "return locals()" to the end of the function and run it through exec. ... def probe: ...
    (comp.lang.python)
  • Re: Self function
    ... Python ... giving the programmer the ability to have a function refer to ... def parrot: ...     print inspect.getmembers ...
    (comp.lang.python)
  • Re: Bush Screwed Up New Orleans Relief Intentionally
    ...   ... Unless the NOLA buses ... But do you hear anyone outside FOX News allowing FEMA off the ... The locals bought shiny beads with the cash the got. ...
    (alt.sports.baseball.ny-mets)
  • Re: What c.l.pys opinions about Soft Exception?
    ... Actually, the latter is even less cluttered, misses a raise - if pure number ... Exception that aren't handled when no handler exists for it. ...     raise_soft ... def a_equal_b: ...
    (comp.lang.python)
  • Re: multiprocessing question/error
    ...     def calcula: ... def m1: ... from multiprocessing import Process, Pool ... def m2(self, arg): ...
    (comp.lang.python)