Python documentation for os.removexattr -- what does the '*' (star) argument mean? [duplicate]
Asked Answered
F

1

6

My first question, please be gentle. I searched but could not find an answer here or elsewhere.

Note that this question does not apply to unpacking of arguments like *args.

In the python 3.3 documentation for os.removexattr the following is stated:

os.removexattr(path, attribute, *, follow_symlinks=True)

    Removes the extended filesystem attribute attribute from path.
    attribute should be bytes or str. If it is a string, it is encoded
    with the filesystem encoding.

    This function can support specifying a file descriptor and not
    following symlinks.

Note that the third argument is a star: *

I assumed that this means "specify one attribute or several attributes separated by comma", but when trying to do that, I get an exception:

import os
os.removexattr('M7-AAE-01.jpg', 'user.camera_brand', 'user.camera_model')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Function takes at most 2 positional arguments (3 given)

I also tried to supply a list of arguments, but that did not work either.

What exactly does the star argument mean in this case? Thank you.

Fulford answered 28/12, 2013 at 2:53 Comment(0)
G
5

The single asterisk * just means that it is forcing you to use named arguments. In this case if you want to pass a value for follow_symlinks, you have to pass the argument name.

The idea is you don't having to read function calls like foo(True, False, False) and not know what those values are doing.

Glasper answered 28/12, 2013 at 3:2 Comment(3)
Thank you for your answer. I have looked at a few more Python documentation examples and realized that the * argument is always used before named arguments, so this is definitely the right answer. I have to say though, it is rather confusing, as it appears that the star would be another argument of some "magic" kind. Instead one should interpret it as "the * argument indicates that any arguments hereafter are named arguments". Something like that...Fulford
@user3122335: well, it seems your observation is not universal; for example os.utime(path, times=None, *, [ns, ]dir_fd=None, follow_symlinks=True). Or is ns a keyword argument here as well?Dol
From the Python reference (docs.python.org/3/reference/…): Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments.Dol

© 2022 - 2024 — McMap. All rights reserved.