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

On Sun, Jun 10, 2012 at 7:01 PM, Steven D'Aprano <steve@pearwood.info>wrote: there should be a more elegant way to do it than having a bunch of specialized functions. What is your use-case for this? "Saving state" is a means to an end. The
As I have mentioned above occasionally I want to turn a list into a single value by some repeated operation, but actually I think its more common that I want to map some operation over a list with dependencies of previous operation passed through. I think my most common use case, is I have a function that operates on a single value, and also has some state. Unfortunately leaving my example purposely vague, I was iterating over a list, and had what was effectively an environment variable (more of state) initially as a dynamic environment, so it was updated each time the function was called across the list comprehension. I then for other reasons wanted to use the environment type as a key for dictionary (which is a problem if its mutable), but that meant that the original list comprehension (which I felt was rather simple). Admittedly it didn't take me any time at all to write a simple function that did the list comprehension, but it still felt like a simple enough problem that it could be elegantly solved without resorting to the helper function.
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.
very neat, thanks!

On Mon, Jun 11, 2012 at 1:09 PM, Andrew Carter <acarter@cs.hmc.edu> wrote:
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 iteration. 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. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Mon, Jun 11, 2012 at 1:09 PM, Andrew Carter <acarter@cs.hmc.edu> wrote:
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 iteration. 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. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (2)
-
Andrew Carter
-
Nick Coghlan