define a new func on the fly?
Christopher Li
chrisl at gnuchina.org
Thu Mar 1 21:02:59 EST 2001
On Thu, 1 Mar 2001, Bruce Edge wrote:
> >>> class xx:
> ... def __init__(self):
> ... exec( "def yyy(): print \"yyyyyyyyy\"" )
> ...
> >>> x=xx()
> >>>
> >>> x.__dict__
> {}
>
> Where is yyy defined?
In local namespace of __init__.
>
> Ideally, I'd like to be able to create it in the global namespace.
Do yon mean in class xx?
How about
>>> class xx:
... def __init__(self):
... exec( "def tmp(self): print 'yyyyyy'")
... self.yyy = tmp
...
>>> x = xx()
>>> x.__dict__
{'yyy': <function tmp at 0x81df40c>}
Chris
More information about the Python-list
mailing list