Verbose and flexible args and kwargs syntax

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Dec 11 17:19:42 EST 2011


On Sun, 11 Dec 2011 11:49:23 +0100, Eelco Hoogendoorn wrote:

> Throwing an idea for a PEP out there:
> 
> It strikes me that the def func(*args, **kwargs) syntax is rather
> unpytonic. It certainly did not have that 'for line in file' pythonic
> obviousness for me as a beginner. Plus, asterikses are impossible to
> google for, so finding out what exactly they do more or less forces you
> to write a forum post about it.

No more so than any other form of punctuation. Plus and minus + - may be 
so common that just about everyone knows it, but how about | == @ % and 
even . (dot)? None of these things will be obvious to newbies who have 
never programmed before. Oh well.

Some things you just have to learn.

As for impossibility of googling for asterisks, it might help if you 
spell it correctly <wink>

http://duckduckgo.com/?q=python+asterisk

Fourth hit says: 

    *vargs puts all left-over non-keyword arguments into a 
    tuple called vargs. **kargs puts all left-over keyword 
    arguments into a dictionary called kargs.

Even better, this works amazingly well:

http://duckduckgo.com/?q=python+*

Or you can Read The Fine Manual, which has a section for special symbols:

http://docs.python.org/genindex-Symbols.html

which is moderately easy to discover from the main page: Main page => 
Documentation => Browse Current Documentation => General Index => 
Symbols. Not ideal, but not too bad.

> A more readable form occurred to me, which also happens to be more
> flexible, and which I think is fully compatible with the syntax of the
> language:
> 
> def func(parg, list(args), dict(kwargs))
> 
> Perhaps this is considered abuse of notation; dict(kwargs) already has a
> meaning rather different from the one we try to give it here; but then
> again the context (being inside a function definition) is unique and
> easily recognizable.

I consider that excessively verbose as well as misleading.

It is excessively verbose for the same reason as:

let y equal x plus 1

would be considered excessively verbose. I'm very fond of some languages 
with verbose syntax, like Hypertalk and OpenXION where you might say "add 
1 to x", but I don't want Python to become them.

It's a judgement call as to where a language divides "cryptic punctuation 
line noise" and "useful short operators", and in my opinion * and ** tuple 
and dict unpacking fall strongly on the "useful short operators" side. 
Your opinion may differ, but luckily for me, the BDFL agrees with me :)

It is also misleading because args are not collected into a list, but 
into a tuple. Worse, it suggests that one should be able to generalise to 
something like this:

def func(parg, str(args), int(kwargs), my_func(more_args)):

which is incoherent.

And lastly, this would require either promoting list and dict to proper 
keywords, which will not happen, or worse, making them semi-keywords 
(treated as keywords in some contexts, but not in others), which also 
will not happen.

> Problems im still wrestling with: the same syntax could not be used when
> calling a function; that lack of symmetry would make things more
> confusing, not less.

Precisely.


> Thoughts?

People learn to spell strings "like this" instead of str(like this), and 
to use # for comments instead of REMARK or REM. It's not that much of a 
burden on them to learn to use * and **


-- 
Steven



More information about the Python-list mailing list