Re:What’s New in Python 3.7
by orf on Sunday September 17, 2017 @11:15AM
(#15269978)
Perhaps it could trigger if you do function(*iterable), with a huge iterable?
[ Parent
| on HN ]
Re:What’s New in Python 3.7
by winterlight on Sunday September 17, 2017 @11:34AM
(#15270066)
Nope, that works just fine.
>>> def f(*x): print(x[-1])
>>> f(range(300))
299
And surprisingly, in python 2.7, I was able to define a function that takes more than 255 arguments. But perhaps this only worked because I cheated and used exec. >>> exec("def f(" + ",".join("f" + str(x) for x in range(300)) + "): print(f299)"
>>> f(*range(300))
299Interestingly enough, the function def (the exec part) does not work in 3.6, as one could expect, but in 2.7 even though the function definition works, you can make the call fail by generating actual parameters:
>>> exec("def f(" + ",".join("f" + str(x) for x in range(300)) + "): print(f299)")
>>> f(*range(300))
299
>>> exec("f(" + ", ".join(str(i) for i in range(300)) + ")")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
SyntaxError: more than 255 arguments
When reducing the number of parameters to be invalid for f, the following error is shown: >>> exec("f(" + ", ".join(str(i) for i in range(30)) + ")")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
TypeError: f() takes exactly 300 arguments (30 given)
Which seems a bit paradoxical - "You need 300 arguments" - "No, wait, actually, you can't have more than 255" ... ;)It looks like they either had changed the internals of python 3, making it fail at the definition as a side effect instead of when calling, or used the same underlying logic but purposely chose to issue the exception to potentially catch the problem earlier...
I think *iterable would just pass one argument that's a list, not pass each argument individually
There may be some optimization at the interpreter level, but it appears to Python that each element of 'iterable' is passed as an individual argument
Re:What’s New in Python 3.7
by fiddlerwoaroof on Sunday September 17, 2017 @11:22AM
(#15270001)
>>> print([1,2,3])
[1,2,3]
>>> print(*[1,2,3])
1 2 3That doesn't tell you anything about when the list is expanded.
Re:What’s New in Python 3.7
by dragonwriter on Sunday September 17, 2017 @11:22AM
(#15270003)
The asterisk at the call site (where it turns an iterable into multiple passed arguments) not the function definition site (where it collects otherwise unconsumed arguments into a single list.)
Re:What’s New in Python 3.7
by Waterluvian on Sunday September 17, 2017 @12:00PM
(#15270207)
I believe args and *kwargs have special opcodes already.