The slash "/" as used in the documentation

Ian Kelly ian.g.kelly at gmail.com
Mon Feb 11 01:45:32 EST 2019


On Sun, Feb 10, 2019 at 2:18 PM Avi Gross <avigross at verizon.net> wrote:
> I am not sure how python implements some of the functionality it does as
> compared to other languages with similar features. But I note that there are
> rules, presumably some for efficiency, such as requiring all keyword
> arguments to be placed after the last positional argument. I mean I can call
> func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2).
>
> So if you allowed a keyword to optionally be used for any positional
> argument other than the last, c, would it not require a change in this rule?
> I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so would
> making b a keyword version. In my example, and for this reason only, maybe c
> could work.

My suggestion was not to allow keyword arguments to arbitrarily
replace any positional parameter, or to otherwise change
argument-passing in any way. The suggestion was to drop
positional-only arguments from functions implemented in C instead of
just documenting them better. In the example above, if you want to
pass a by keyword, you would have to pass b and c by keyword as well.
That would still be true for functions implemented in C if a, b, and c
are no longer positional-only.

> The original motivation for keyword arguments included the concept of
> specifying a default if not used. Positional arguments have no default.
> Similarly, they are required if explicitly named in the function definition.
> So we are intermingling multiple concepts in the previous design.

Positional arguments are allowed to have defaults, and keyword-only
arguments are allowed to not have defaults. These are all valid
syntax:

# Allows a and b to be passed either positionally or by keyword
def foo(a=1, b=2): pass

# b is keyword-only
def foo(a=1, *, b=2): pass

# Allows a and b to be passed either positionally or by keyword
def foo(a, b): pass

# b is keyword-only and has no default
def foo(a, *, b): pass

Positional-ONLY arguments are not directly supported by the language,
but they exist in the C API and can also have defaults. For example,
dict.get takes two positional-only arguments. The second argument is
optional, and its default is None.

My point is that the axis of positional-only versus
positional-or-keyword versus keyword-only, and the axis of required
versus optional are entirely separate.

> I won't go on except to say it would break lots of existing code and
> potentially complicate new code.

Can you give an example of something that would be broken by updating
C API functions to name positional-only arguments instead of just
updating them to document that they're positional-only?



More information about the Python-list mailing list