Hacker News new | past | comments | ask | show | jobs | submit login

I dont like the positional only arguments..

Really, I dont like anything that trys to force a future developer into using your code the way you expect them to.




One of the use-cases for positional-only arguments strikes me as being very sensible:

    def my_format(fmt, *args, **kwargs):
        ...
        fmt.format(*args, **kwargs)
suffers from a bug if you want to pass fmt as a keyword argument (e.g. `my_format('{fmt}', fmt='int')`). With positional-only arguments that goes away.

You could always force developers into using your code the way you expect by parsing args/kwargs yourself, so it's not like this really changes anything about the "restrictiveness" of the language.


I think the main value is that function documentation becomes slightly less absurd.

If you run `help(pow)` as early as Python 3.5 it lists the signature as `pow(x, y, z=None, /)`. The first time I saw that `/` I was pretty confused, and it didn't help that trying to define a function that way gave a syntax error. It was this weird thing that only C functions could have. It's still not obvious what it does, but at least the signature parses, which is a small win.

Another thing it's good for is certain nasty patterns with keyword arguments.

Take `dict.update`. You can give it a mapping as its first argument, or you can give it keyword arguments to update string keys, or you can do both.

If you wanted to reimplement it, you might naively write:

  def update(self, mapping=None, **kwargs):
      ...
But this is wrong. If you run `d.update(mapping=3)` you won't update the 'mapping' key, you'll try to use `3` as the mapping.

If you want to write it in pure Python < 3.8, you have to do something like this:

  def update(*args, **kwargs):
      if len(args) > 2:
          raise TypeError
      self = args[0]
      mapping = None
      if len(args) == 2:
          mapping = args[1]
      ...
That's awful.

Arguably you shouldn't be using keyword arguments like this in the first place. But they're already used like this in the core language, so it's too late for that. Might as well let people write this:

  def update(self, mapping=None, **kwargs, /):
      ...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: