1

In the NumPy Manual, there are objects having a wildcard as argument as shown in the title of this question.

First, I have no idea what it means in this context.

Second, when trying to redefine the function as such:

def array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None):
    if MODE == 'CPU':
        return np.array(object, dtype, *, copy, order, subok, ndmin, like)

print (array([1,2,3]))

I get the error:

  File "test.py", line 18
    return np.array(object, dtype, *, copy, order, subok, ndmin, like)
                                    ^
SyntaxError: invalid syntax

Although I know what *args or **kwargs mean, I have no idea what * means. Searching google for this does not yield to satisfying results. Any explanation would be appreciated.

Synthase
  • 5,849
  • 2
  • 12
  • 34
  • 2
    Take a look at sections [4.6 Defining Functions](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) and [4.7. More on Defining Functions](https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions) in the Python tutorial. Specifically, [4.7.3 Special Parameters](https://docs.python.org/3/tutorial/controlflow.html#special-parameters) covers the meaning of an isolated `*` in the function signature. – Warren Weckesser Jul 13 '21 at 20:51
  • The syntax error that you got is probably because you are using a version of Python that is older than the minimum supported by your NumPy version. – Warren Weckesser Jul 13 '21 at 20:53
  • You don't use it when calling the function. It's ok in the signature. `return np.array(object, dtype, copy, order, subok, ndmin, like)` – hpaulj Jul 13 '21 at 20:55

2 Answers2

1

The thing is in call all values after * have to be put with a name of an argument.

Example with error as a result because third parameter is not provided with name of argument:

def f(a=1,b=2,*,c=3):
    print(a)
    print(b)
    print(c)
f(1,2,3)

Example without the error:

def f(a=1,b=2,*,c=3):
    print(a)
    print(b)
    print(c)
f(1,2,c=3)

Here is similar topic.

0

All the arguments that come after the * are keyword only arguments. They must be specified as a keyword rather than by position. The * functions to specify that positional arguments will not be interpreted as any of the keyword arguments following it such as copy or order in this case and will throw an error if you try to specify more positional arguments than precede the *. See this PEP Document for more, particularly the example in the Specification section

C Haworth
  • 659
  • 3
  • 12