Verbose and flexible args and kwargs syntax

Chris Angelico rosuav at gmail.com
Sun Dec 11 08:09:56 EST 2011


On Sun, Dec 11, 2011 at 11:39 PM, Duncan Booth
<duncan.booth at invalid.invalid> wrote:
> Chris Angelico <rosuav at gmail.com> wrote:
> If it used keywords then you could keep symmetry quite easily:
>
>    def anyargs(arglist args, argdict kwargs):
>        return wrappedfunc(arglist args, argdict kwargs)
>
> and you would have the advantage of two new keywords that people could
> actually search on.

Yes, that's just a strict keywordification of the * and ** symbols.
The same argument could be made for eliminating the standard algebraic
+ operator and replacing it with a keyword "__add__". I don't think
that's worthwhile.

The OP suggested using 'dict' and 'list' themselves as the keywords,
thus allowing the use of subclasses. This would make it unsearchable,
or else rather verbose:

def anyargs(pack list args, pack dict kwargs):
    return wrappedfunc(pack list args, pack dict kwargs)

With this syntax, what happens if you muck up list/dict? Or
alternatively, the briefer syntax:

def anyargs(pack list args, pack dict kwargs):
    return wrappedfunc(pack args, pack kwargs)

 which breaks the symmetry, and doesn't say which one you're doing -
for instance, if you only use one out of list and dict args, it would
make sense to have a variable "options" or "args" that gets unpacked
to the function's arguments - and there's no way to see whether it's
going to be keyword or positional.

The verbose syntax has a lot going for it, but it's rather verbose.
Why say "pack list args" when you can just say "*args"? Compare
initializer syntax between Python and PHP:

foo = ["asdf", "qwer", {1:2, 3:4}]

$foo = array("asdf", "qwer", array(1=>2, 3=>4))

Is it more Pythonic to use explicitly-named types, or to have simple
notation that's clear and easy to read? Or is this a matter for
personal preference?

>> Another issue: You suggest being able to use "attrdict" or some other
>> dict subclass. This means that, rather than being a language
>> construct, this will involve a name lookup. And what happens if you
>> have a class that subclasses both list and dict? Will it get the
>> positional args, the keyword args, or both?
>
> Irrelevant, you can't subclass both list and dict:
> TypeError: multiple bases have instance lay-out conflict

Ah. Curious. I've not done much with multiple inheritance in Python
(come to think of it, I don't recall when I last used MI in _any_
language). In any case, there's still the potential unclarity as to
_which_ of dict and list is the one that's been inherited, which
requires a run-time lookup to solve.

ChrisA



More information about the Python-list mailing list