[Python-ideas] Positional only arguments
Steven Bethard
steven.bethard at gmail.com
Fri May 18 23:07:22 CEST 2007
On 5/16/07, George Sakkis <george.sakkis at gmail.com> wrote:
> I see the utility of positional-only arguments, not just
> for the sake of symmetry but for pragmatic reasons; in fact the same
> reasons that serve as rationale to the keywords-only PEP.
In the hopes of keeping this discussion enough on track that we can
eventually come to a conclusion, I thought I'd just take a couple
minutes to summarize the proposals so far. I'm looking at the
following function as an example::
def f(a, b=None, *, c=42, **kwargs)
where ``a`` and ``b`` should be positional-only and ``c`` should be
keyword-only. Here are the alternatives I've seen so far:
* Do nothing, e.g.::
def (*args, c=42, **kwargs):
if len(args) == 1:
a = args[0]
b = None
elif len(args) == 2:
a, b = args
else:
raise TypeError()
Pro: backwards compatible, requires no new syntax
Con: unfriendly to introspection, requires extra boiler-plate
* Enforce double-underscore names as being positional-only syntactically, e.g.::
def f(__a, __b=None, *, c=42, **kwargs)
Pro: requires no new syntax
Con: slightly backwards incompatible (but who calls f(__a) anyway?)
* Use a decorator, e.g.::
@posonly(2)
def f(a, b=None, *, c=42, **kwargs)
Pro: backwards compatible, requires no new syntax, could work with Python 2.5
Con: moves some signature info out of the argument list
* Make the lone '*' force everything to the left to be positional-only
and everything to the right be keyword-only, e.g.::
def f(a, b=None, *, c=42, **kwargs):
Pro: backwards compatible (since lone '*' is not yet introduced),
requires no new syntax
Con: disallows named arguments with keyword-only arguments,
disallows positional-only arguments with *args.
* Reuse syntax like what was removed for tuple arguments, e.g.::
def f((a, b=None), *, c=42, **kwargs):
Pro: ?
Con: changes meaning of tuple argument syntax
* Add a new delimiter, e.g.::
def f(a, b=None, %, *, c=42, **kwargs)
def f(a, b=None; *, c=42, **kwargs)
Pro: backwards compatible
Con: requires new syntax, in second version semi-colon vs. comma is
hard to spot
So at the moment, it looks to me like the double-underscores and the
decorator are the top competitors.
STeVe
--
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
--- Bucky Katt, Get Fuzzy
More information about the Python-ideas
mailing list