[Python-ideas] Fwd: quantifications, and tuple patterns
Paul Moore
p.f.moore at gmail.com
Sat Jan 14 17:19:29 CET 2012
On 14 January 2012 13:57, Annie Liu <liu at cs.stonybrook.edu> wrote:
> For those two simplest examples, yes, "any" and "all" can be used.
> However, there are two general problems.
>
> 1. The order of coding using "any/all" is the opposite order of
> thinking in one's head. In our experience, those kinds of simple
> examples are coded much more often using "len" than "any/all". The
> ordering problem gets worse when quantifications are needed in more
> complex conditions or are nested.
To be honest, that's a matter of opinion. I find any/all with a
generator expression more readable than your syntax, because I am used
to generator expressions as they are common in Python.
> 2. The much worse problem is when a witness is needed from existential
> quantification. In my last/third example from a distributed algorithm,
> this is the desired code (recall that ! indicates a bound variable):
>
> while some (!slot_num,p1) in decisions:
> if some (!slot_num,p2) in proposals has p2 != p1:
> propose(p2)
> perform(p1)
Wow, I find that unreadable!. The ! doesn't read as any sort of
assignment to me. Don't forget that in Python, assignments are
statements and don't get embedded in expressions. There is no way
you're going to get new syntax that creates an expression which embeds
a hidden assignment accepted into Python.
I'd offer to re write this in idiomatic Python, but looking at it
closer, I've no idea how to. I don't know what slot_num is meant to
be, at first I thought p1 and p2 were predicates, but then I notice
you are comparing them later.
Can I suggest you write your example out in Python that works today,
and then show how it looks with your proposed syntax alongside? If you
can't find the "best" way of writing it in existing Python, just write
it however works, no need to try to make it compact, or elegant.
There'll be plenty of people here who will show you how to write
idiomatic Python versions of what you post :-)
Paul.
More information about the Python-ideas
mailing list