eval() and global variables

rdmurray at bitdance.com rdmurray at bitdance.com
Tue Dec 16 18:59:31 EST 2008


Quoth "=?ISO-8859-1?Q?Juan_Pablo_Romero_M=E9ndez?=" <jpablo.romero at gmail.com>:
> Hello,
> 
> Suppose this function is given:
> 
> def f(x,y):
>   return x+y+k
> 
> Is it possible to somehow assign a value to k without resorting to
> making k global?
> 
> I'm thinking something like this:
> 
> eval("f(1,1)", {"f":f, "k":1})
> 
> Or even better, something like:
> 
> def g(k):
>   return f
> 
> g(1)(1,1) ==> 3

    >>> def g(k):
    ...     def f(x,y):
    ...             return x+y+k
    ...     return f
    ... 
    >>> g(1)(1,1)
    3

But what's the use case?  The above might not satisfy it.

--RDM




More information about the Python-list mailing list