In comparison, I think that := is much simpler.
--
---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
--
---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
ChrisA
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
z = {a: transformed_bfor b in bsgiven transformed_b = transform(b)for a in as_}There is no nice, equivalent := version as far as I can tell.
On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar <miste...@gmail.com> wrote:
> This example shows additional flexibility:
>
> z = {a: transformed_b
> for b in bs
> given transformed_b = transform(b)
> for a in as_}
>
> There is no nice, equivalent := version as far as I can tell.
True. However, it took me several readings to understand what you were
doing here. I think I actually prefer "for transformed_b in
[transform(b)]" to this syntax, which is saying something.
ChrisA
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
On Wed, May 30, 2018 at 01:59:37PM -0400, Neil Girdhar wrote:
> This example shows additional flexibility:
>
> z = {a: transformed_b
> for b in bs
> given transformed_b = transform(b)
> for a in as_}
Is that even legal?
--
Steve
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
On Thu, May 31, 2018 at 04:06:51AM +1000, Chris Angelico wrote:
> On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar <miste...@gmail.com> wrote:
> > This example shows additional flexibility:
> >
> > z = {a: transformed_b
> > for b in bs
> > given transformed_b = transform(b)
> > for a in as_}
> >
> > There is no nice, equivalent := version as far as I can tell.
>
> True. However, it took me several readings to understand what you were
> doing here.
Possibly you shouldn't have tried reading at 4am.
Either that or I shouldn't be reading before I've had a coffee :-)
Have I missed something that you have seen? Even if the syntax were
legal, that seems to be a pointless use of an assignment expression.
Since the new name "transformed_b" is only used once, we can and should
just use the transform(b) in place:
z = {a: transform(b) for b in bs for a in as_}
If we need to use it twice, we can do this:
# assume "@" stands in for something useful
z = {a: (transformed_b := transform(b)) @ transformed_b
for b in bs for a in as_}
I'm not seeing the advantage of given, or any extra flexibility here,
unless the aim is to encourage people to make syntax errors :-)
What have I missed?
--
Steve
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
[expression given name=something for x in seq]
[expression for x given name=something in seq]
[expression for x in seq given name=something]
[expression for x in seq if given name=something condition]
[expression for x in seq if condition given name=something]
--
Steve
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
[expression given name=something for x in seq]retval = []name = somethingfor x in seq:retval.append(expression)return retval
[expression given name=something for x in seq]retval = []name = somethingfor x in seq:retval.append(expression)return retval
retval = [expr(name) given name=something(x) for x in seq]
Well, there need not be any ambiguity if you think of "B given A" as "execute A before B", and remember that "given" has a lower precedence than "for" (So [B given A for x in seq] is parsed as [(B given A) for x in seq]Then
retval = [expr(name) given name=something(x) for x in seq]Is:retval = []for x in seq:name = something(x)retval.append(expr(name))And
retval = [expr(name, x) for x in seq given name=something]Is:retval = []name = somethingfor x in seq:retval.append(expr(name, x))
But this is probably not a great solution, as it forces you to mentally unwrap comprehensions in a strange order and remember a non-obvious precedence rule.
On the plus-side, it lets you initialize generators with in-loop updates (which cannot as far as I see be done nicely with ":="):retval = [expr(name, x) given name=update(name, x) for x in seq given name=something]Is:retval = []name = somethingfor x in seq:name = update(name, x)retval.append(expr(name, x))
Why wouldn't you want to just put the outer given outside the entire comprehension?retval = [expr(name, x) given name=update(name, x) for x in seq] given name=something
The more I think about it, the more i want to keep "given" in comprehensions, and given in expressions using parentheses when given is supposed to bind to the expression first.
# alternate existing solution
[process(tx, y) for x in xs for tx in [transform(x)] for y in yz]
This syntax allows you to use both x and tx in the resultant
expression. For instance:
[process(value, row_total) for row in dataset for row_total in
[sum(row)] for value in row]
ret = []
for row in dataset:
row_total = sum(row)
for value in row:
ret.append(process(value, row_total))
If done without optimization, each row would take O(n²) time, but this
way it's O(n).
I think Serhiy was trying to establish this form as a standard idiom,
with optimization in the interpreter to avoid constructing a list and
iterating over it (so it would be functionally identical to actual
assignment). I'd rather see that happen than the creation of a messy
'given' syntax.
ChrisA
[process(tx, y) for x in xs for tx in [transform(x)] for y in yz]
...
I think Serhiy was trying to establish this form as a standard idiom,
with optimization in the interpreter to avoid constructing a list and
iterating over it (so it would be functionally identical to actual
assignment). I'd rather see that happen than the creation of a messy
'given' syntax.