Ah actually the keyword in the second example is parsed as a third arg of the generator expression, which seems to be ignored, so the whole thing acts as if no keyword had been set. That’s why this works as well sum(0.0 for i in [], init = 0.0; init = 0.0), so the generator doesn’t care about this syntax appendage. I would think this is a parsing bug then.
julia> Meta.@dump sum(0.0 for i in [], init = 0.0)
Expr
head: Symbol call
args: Array{Any}((2,))
1: Symbol sum
2: Expr
head: Symbol generator
args: Array{Any}((3,))
1: Float64 0.0
2: Expr
head: Symbol =
args: Array{Any}((2,))
1: Symbol i
2: Expr
head: Symbol vect
args: Array{Any}((0,))
3: Expr
head: Symbol =
args: Array{Any}((2,))
1: Symbol init
2: Float64 0.0
In [2]: sum(1 for _ in [], start=0)
File "<ipython-input-2-646ca842f927>", line 1
sum(1 for _ in [], start=0)
^
SyntaxError: Generator expression must be parenthesized
In [3]: sum((1 for _ in []), start=0)
Out[3]: 0
I told you, I forgot what they are because I don’t run into them anymore
But for example the syntax f(; a) for f(; a=a) works only if you explicitly use the semicolon. Or also slurping all the keyword arguments, f(x; kwargs...), works only with semicolon and not the colon. There are probably other things that wouldn’t work but I just don’t remember them. I’m pretty sure I already ran into the issue reported at the top of this thread, before stopping using the comma. My suggestion is: always use the semicolon to start the list of keyword arguments. By “always” I mean literally always, also when there are no positional arguments.
But apparently only in that case, because this works without parentheses:
>>> sum(1 for i in [1,2,3])
3
But I’m not sure why Python requires parentheses when keyword arguments are involved, because the language doesn’t seem to allow multidimensional generators like Julia does:
>>> (1 for i in [1,2,3], j in [1,2])
File "<stdin>", line 1
(1 for i in [1,2,3], j in [1,2])
^
SyntaxError: invalid syntax
so it seems like there is no ambiguity to resolve by requiring parentheses.
Thanks. I am also in the habit of always using the colon, albeit for the more superficial reason that doing so forces me to remind myself of how I defined my functions and keep them consistent.