[Python-ideas] Fwd: quantifications, and tuple patterns
Nick Coghlan
ncoghlan at gmail.com
Sun Jan 15 03:27:10 CET 2012
More information about the Python-ideas mailing list
Sun Jan 15 03:27:10 CET 2012
- Previous message: [Python-ideas] Fwd: quantifications, and tuple patterns
- Next message: [Python-ideas] Fwd: quantifications, and tuple patterns
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, Jan 15, 2012 at 9:27 AM, Paul Moore <p.f.moore at gmail.com> wrote: > Agreed. Assignment-as-expression is nicer. But I have to say I've > never really missed it in Python until this discussion started. > > Question. Do you object to assignment-as-expression per se, or merely > as the default way of looking at assignment? Or to put it another way, > would something like my x := val (or maybe x <- val or something else) > be acceptable? Just to make sure I have the example right, I believe this is what the original example looks like without using comprehensions at all: for dec_slot, p1 in decisions: if dec_slot == slot_num: for prop_slot, p2 in proposals: if prop_slot == slot_num and p2 != p1: # Do something! You can merge the for loops and the if statements, and hence avoid the naming conflict for the slot variables with a couple of generator expressions (there's apparently no need to waste memory realising the set for this example). You can also trivially adopt a mathematics style ordering for any() and all() by using "True" as the body and moving the predicate to the comprehension's if clause. That means, the example can already be written as: for p1 in (p for slot, p in decisions if slot == slot_num): if any(True for slot, p2 in proposals if slot == slot_num and p2 != p1): # Do something! The tricks are: 1. Use generator expressions in order to get at the values as they're produced, rather than realising the sets in memory 2. Use a for loop instead of a while loop in order to capture those values 3. Use "True" with any()/all() to get a mathematical style quantification ordering of expressions Cheers, Nick. P.S. My reply appears in this part of the thread because it started out as a reference to my previous suite expressions concept [1] that supports embedded assignment statements in a way that allows the value saved and the value used as a predicate to differ. As I wrote it up though, I realised that the specific example given could be translated fairly neatly into *existing* Python constructs, as shown above. [1] http://ncoghlan_devs-python-notes.readthedocs.org/en/latest/pep_ideas/suite_expr.html -- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
- Previous message: [Python-ideas] Fwd: quantifications, and tuple patterns
- Next message: [Python-ideas] Fwd: quantifications, and tuple patterns
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list