Understanding Slash and Subsequent Meaning in the Numpy Reference Function Arguments

Asked 2 years ago, Updated 2 years ago, 47 views

Thank you for your help.

I have a question about the title.

Describes the meaning of the reference sin function argument in numpy v1.15.
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.sin.html#numpy.sin

For your reference, see

numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[,signature,extobj])=

It says, but

For actual use
sin(x,*args,**kwargs)

It must be said that
I checked the numpy source code umath.py from pycharm, but
defsin(x,*args,**kwargs):
defined in the and

numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[,signature,extobj])=
The section was commented out in the lower line.

Why is the documentation
numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[,signature,extobj])=
I am at a loss because I do not understand the meaning of the description.

I understand the x of the first argument, but

second and subsequent arguments /, I don't know how to check out, *, where, casting, order, etc.

I'm very sorry for the confusing question, but
Does anyone know what part of the numpy manual I should look at to understand how to look around here?

python numpy

2022-09-30 17:25

1 Answers

Almost everything is explained in the link in the comment, but I will leave a reply in Japanese for your reference.

This notation is not unique to numpy, but is traditionally used in Python documentation.Originally, this was a notation to describe the unique specifications of Python functions implemented in C, but since Python 3.8 can now be used with pure Python code, I will explain based on that.

In the middle of an argument, / is a marker that divides the arguments of a function definition to the left and right, and does not actually pass the argument /.In fact, there has always been a delimited mark * on something similar.Roughly speaking, it means:

    The arguments to the left of
  • / are positional-only parameters and must be passed based on the order of the arguments when calling a function, not on keywords (like f(x=42) using the name of the provisional argument.
  • Arguments to the right of
  • * are keyword-only parameters and must be passed with keywords.
  • Arguments between
  • / and * can be specified by location or keyword (typically this is the default).

PEP570 for a specific example.

def standard_arg(arg):
    print(arg)

def pos_only_arg(arg, /):
    print(arg)

defkwd_only_arg(*,arg):
    print(arg)

def combined_example(pos_only, /, standard, *, kwd_only):
    print(pos_only, standard, kwd_only)

When there is such a function, the first function can be called either standard_arg(2) or standard_arg(arg=2), but the second function is an error if pos_only_arg(arg=1).Similarly, if the third function is kwd_only_arg(3), it will fail.The last example is a mixed pattern, and the relative position between / and * determines how the arguments are passed.

For more information, see the PEP570 or the Python 3.8 Release Notes location-only argument item .


2022-09-30 17:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.