1

From the docs

Due to an oversight, earlier Python versions erroneously accepted the following syntax:

f(1 for x in [1], *args)
f(1 for x in [1], **kwargs)

Python 3.5 now correctly raises a SyntaxError, as generator expressions must be put in parentheses if not a sole argument to a function.


What does that mean? Why is it that it's the wrong with the syntax above? Can someone show an example code of why is that a bad generator expression?

I think I understand it this much:

  • there is a function f()
  • that takes a generator, args and kwargs as its parameter

But why is the args and the kwargs usage wrong?

alvas
  • 115,346
  • 109
  • 446
  • 738
  • Background (just in case anyone asks): I'm debugging why the `nltk` code for `mwa_ppdb` works in Python < 3.5 but in Python 3.5 breaks when the kwargs somehow disappears when an object is imported with absolute import. I suspect it has to do with the changes from Python 3.4 -> 3.5. More details on https://github.com/nltk/nltk/pull/1627 – alvas Feb 16 '17 at 00:38
  • 1
    The generator expression by itself is fine, the problem is that if it is not the *sole argument* to a function, then it must be in parentheses. This is part of the document grammer. – juanpa.arrivillaga Feb 16 '17 at 00:41
  • Thanks @juanpa.arrivillaga !! – alvas Feb 16 '17 at 00:43

0 Answers0