1 Python 2.5.2 (r252:60911, Oct 5 2008, 19:29:17) 2 [GCC 4.3.2] on linux2 3 Type "help", "copyright", "credits" or "license" for more information. 4 5 >>> def f(*args): 6 ... print ' '.join([str(a) for a in args]) 7 ... 8 >>> f(1,2,3) 9 1 2 3 10 >>> f(1,*(2,3)) 11 1 2 3 12 >>> f(*(1,2),3) 13 File "<stdin>", line 1 14 f(*(1,2),3) 15 ^ 16 SyntaxError: invalid syntax
Sigh.
Tags: consistency, Python
December 4, 2008 at 11:49 am |
No use sighing. The asterisk form has a specific meaning, and is specifically required to be the last argument. It isn’t Python’s fault that tou won’t read, or won’t follow, the documentation.
December 4, 2008 at 1:06 pm |
Steve > Python 3 allow this kind of syntax, and python has been realased this days, so perhaps this post have a relation with it and our friend use sighing because he can’t wait to use it…
December 4, 2008 at 1:35 pm |
@ Guillaume: thanks for the pointer, see: http://muharem.wordpress.com/2008/12/04/this-is-much-better-indeed/
December 4, 2008 at 1:37 pm |
[...] Muharem Hrnjadovic Cool ideas revolving around computers and programming « Why is consistency so difficult to achieve? [...]
December 4, 2008 at 1:56 pm |
“Python 3 allow this kind of syntax”
Well, no, but it does allow f(*(1,2), c=3) and it has a better error message.
The Python2 solution is of course to either expand the tuple:
t = (1,2)
f(t[0], t[1], 3)
Or add the third parameter to the tuple:
f(*(1,2)+(3,))
December 4, 2008 at 2:31 pm |
Although he could be nicer about it, Steve’s right. The (a, b, c, *args, **kwargs) pattern is both well-documented and well known. And, incidentally, someone matching the Python idea that there should be only one way to do it.
December 4, 2008 at 4:27 pm |
@ Steve and J.T.
It’s well documented indeed. But you have to admit that in Python 3 it’s *even better* documented. Documentation is not religion