static variables in Python?

Alan Franzoni alan.franzoni.blahblah at example.com.invalid
Wed Jul 30 04:15:15 EDT 2008


kj was kind enough to say:

> In this case, foo is defined by assigning to it a closure that has
> an associated variable, $x, in its scope.
> 
> Is there an equivalent in Python?

There've been plenty of answers, and I'm not absolutely sure about what you
want... but closures are available in Python as well and you can use them,
and by combining them through the partial module you can get a sort of
closure factory:

from functools import partial

def getfunc(expensive_call, myfunc):
    val = expensive_call()

    f = partial(myfunc, val)

    return f



you can then do something like that:

>> f = getfunc(lambda: 1, lambda x,y:x*y)
>> f(2)
6





-- 
Alan Franzoni <alan.franzoni.xyz at gmail.com>
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E



More information about the Python-list mailing list