Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16979 > unrolled thread

Verbose and flexible args and kwargs syntax

Started byEelco Hoogendoorn <hoogendoorn.eelco@gmail.com>
First post2011-12-11 11:49 +0100
Last post2011-12-11 22:19 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Verbose and flexible args and kwargs syntax Eelco Hoogendoorn <hoogendoorn.eelco@gmail.com> - 2011-12-11 11:49 +0100
    Re: Verbose and flexible args and kwargs syntax Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-11 22:19 +0000

#16979 — Verbose and flexible args and kwargs syntax

FromEelco Hoogendoorn <hoogendoorn.eelco@gmail.com>
Date2011-12-11 11:49 +0100
SubjectVerbose and flexible args and kwargs syntax
Message-ID<mailman.3509.1323600572.27778.python-list@python.org>
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.

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.

An added advantage would be the possibility of using subclasses of dict 
and list as well; imagine how much more beautiful a lot of code would be 
if one could say

def func(attrdict(kwargs))

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.

Thoughts?

[toc] | [next] | [standalone]


#16989

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-11 22:19 +0000
Message-ID<4ee52c7e$0$11091$c3e8da3@news.astraweb.com>
In reply to#16979
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web