[Python-3000] Type Expressions
Sam Pointon
free.condiments at gmail.com
Thu Apr 20 17:16:55 CEST 2006
On 20/04/06, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> So maybe you should be able to write that as
>
> def spam{d, e, f=x}:
> ...
>
> Then if you have some positional args as well,
>
> def spam(a, b, c){d, e, f=x}:
> ...
>
> And incorporating all the possible features,
>
> def spam(a, b, c=y, *d){e, f=x, **g}:
> ...
>
> Although now that the ** arg is inside the {}, there's
> no longer any need for the double *, so it could just be
>
> def spam(a, b, c=y, *d){e, f=x, *g}:
> ...
Following on from this, how about using [] for positional-only
arguments as well? * and ** arguments will follow the same rules as
they do now - {} for keyword-only arguments obliviates the need for
keyword arguments after a * argument. ** will not be allowed in [].
Then, we get this legal function definition:
def foo[a, b, c = 'spam'](d, e = 'ham', *f){g, h = 'eggs', **i):
...
which, IMO, looks natural, and I think is reasonably intuitive ([] for
lists, so positional only, {} for dicts, so keyword only, and () like
it is now, so no surprises there). Also, none of these are nested
inside each other (barring pattern matching on tuples) so Guido's
concern about nested brackets is assuaged. Also, all three sections
will be optional, so long as there is at least one of them.
If I had any real experience past minor tinkering with CPython's
internals I'd have a go at implementing this per Guido's appetite for
code, but this change will affect not just the parser/compiler, but
also the bytecode interpreter at the very least. However, I will be
looking in my code and the stdlib for use cases for this idea.
--Sam
More information about the Python-3000
mailing list