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

Duncan Booth duncan at
Thu Aug 8 10:13:36 CEST 2002

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

> 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 

>>> 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 ?
  File "<pyshell#9>", line 2, in test
    raise 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
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