From: eryk sun <eryk...@gmail.com>
Sent: Monday, October 15, 2018 12:10 PM
To: tutor@python.org
Cc: sjeik_ap...@hotmail.com
Subject: Re: [Tutor] what does the forward slash mean in this function 
signature?
  

On Mon, Oct 15, 2018 at 6:00 AM Albert-Jan Roskam
<sjeik_ap...@hotmail.com> wrote:
>
> In Python 3.6 (Windows) I often see a forward slash in a function signature, 
> see below.
> What does it mean? I vaguely remember reading something about new 
> possbilities in
> python 3, something like "def foo(x, *, y)". Perhaps it's related to that?
>
> >>> help({}.fromkeys)
> Help on built-in function fromkeys:
>
> fromkeys(iterable, value=None, /) method of builtins.type instance
>     Returns a new dict with keys from iterable and values equal to value.

This syntax is for position-only function parameters. It's used by
Argument Clinic [1], a preprocessor used internally to develop
CPython. Position-only parameters have also been proposed for the
language grammar, first in PEP 457 and again in PEP 570 [2].

[1]:  https://docs.python.org/3/howto/clinic.html#converting-your-first-function
[2]: https://www.python.org/dev/peps/pep-0570



========> Hi Eryk,

Thank you, that's interesting to know. I wasn't even aware of position-only 
parameters until I read about them. Of course I've used ord() many times, I 
just thought it has a keyword argument. Why even bother to have positional-only 
params? I agree that ord('a') reads more easily than ord(character='a'), but 
other than that I don't see an advantage to enforce it.

The pages you mentioned also made me read about function annotations and type 
hints. Do you know why type hints were designed while there are already are 
parseable docstring notations (sphinx  :param foo: int, numpydoc (much prettier 
IMHO), and others)? The type hints remind of Cython, but "Type annotations 
should not be confused with variable declarations in statically typed 
languages. The goal of annotation syntax is to provide an easy way to specify 
structured type metadata for third party tools." [1]

Thanks!

Albert-Jan

[1]  https://www.python.org/dev/peps/pep-0526/
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to