Statespressions? (Re: Draft Pep (was: Re: Let's Talk About Lambda Functions!))

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Aug 8 04:13:36 EDT 2002


tanzer at swing.co.at (Christian Tanzer) wrote in 
news:mailman.1028787078.20544.python-list at python.org:

> Now that's easy:
> 
>     >>> def test() :
>     ...   print "hello"
>     ...
>     >>> x = new.function (test.func_code, globals (), "'case a'")
>     >>> x
>     <function 'case a' at 0x822ae24>
>     >>> x()
>     hello
> 
> Ducking-for-cover-ly yrs,
> 

Note that although x.func_name is 'case a', if it should throw an exception 
the stack backtrace will *still* refer to 'test'. func_code has its own 
name!

>>> def test():
	raise RuntimeError

>>> x = new.function (test.func_code, globals (), "'case a'")
>>> x.func_name
"'case a'"
>>> x()
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in ?
    x()
  File "<pyshell#9>", line 2, in test
    raise RuntimeError
RuntimeError

You can probably build a new code object using new.code(), but with 14 
arguments to be passed in I can't be bothered showing an example.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list