[Python-ideas] Saving state in list/generator comprehension

Nick Coghlan ncoghlan at gmail.com
Mon Jun 11 08:45:46 CEST 2012

On Mon, Jun 11, 2012 at 1:09 PM, Andrew Carter <acarter at cs.hmc.edu> wrote:
>  I feel like there is a need from personal experience of mapping with state
> in a short concise way. However it is quite possible that it is just me, and
> I need to think about the problem differently, or perhaps live with 4 line
> functions that are only used once. As for the backwards compatibility I
> think was getting ahead of myself, I feel the with/as solution is quite
> clunky, but I couldn't come up with a more elegant solution that operated in
> a similar vein to how python feels as a language.

Part of how Python feels as a language is due to the fact that
stateful operations cannot, in general, be expressed cleanly as
expressions - you have to step up to a multi-statement procedural
algorithm if your state can't be expressed cleanly through simple

I and others have put forward various proposals to change this over
the years, but it's a complex problem that touches on the heart of the
statement/expression dichotomy that Guido deliberately introduced when
creating the language.

The mechanism I personally consider most promising is one that makes
it easier to be explicit that a particular function is only used in
the current statement (see PEP 403). It still feels like Python (i.e.
no embedded assignments), but also clearly expresses when a function
exists solely for code structure purposes, and has nothing to do with
splitting out a component that will be used from multiple locations.

The current design proposal in PEP 403 is still quite flawed, though,
and needs a substantial amount of work to be brought up to a standard
where it makes a compelling case for a change to Python.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list