Inline Calculation of Dictionary Values
Chris Rebert
clp2 at rebertia.com
Wed Apr 21 09:10:55 EDT 2010
On Wed, Apr 21, 2010 at 5:51 AM, ++imanshu <himanshu.garg at gmail.com> wrote:
> Is it possible to something along these lines in python :-
>
> map = {
> 'key1': f(),
> 'key2': modify_state(); val = f(); restore_state(); val,
> 'key3': f(),
> }
>
> For 'key2' I want to store the value returned by f() but after
> modifying the state. Do we have something like a "bare block".
Based on what I can find about "bare blocks", Nope. And we like it that way :-)
> I am
> trying to avoid this :-
>
> def f2():
> modify_state()
> val = f()
> restore_state()
> return val
>
> map = {
> 'key1': f(),
> 'key2': f2()
> 'key3': f(),
> }
FWIW, I don't see what's wrong with this. You could probably refactor
f2() to use the `with` statement and a context manager, but that's
getting tangential.
However, the question arises: Why do you have global state in the first place?
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list