r/learnpython • • Feb 11 '25

I think positional-only and keyword-only arguments syntax sucks

This is my mini rant, please don't take it super seriously.

I don't quite understand it why people who develop the Python language feel the urge to make it more and more complex, adding features nobody asked for. Someone can say "but you don't need to use them". Well, sure, but I need to have them sometimes when I work on a project with other devs.

One of the best examples is the positional-only and keyword-only syntax. I love it that Python supports keyword arguments, but forcing to use them seems like something nobody really needs. And positional-only even more so.

But now, I'm gonna talk about the syntax itself:

def my_func(a, b, /, c, d, *, e, f):
    # a and b are now positional-only
    # c and d are whatever we want
    # e and f are keyword-only
    pass

It takes quite a bit of mental power to acknowledge which arguments are what. I think it would really be better if each parameter was marked appropriately, while the interpreter would make sure that positional-only are always before keyword-only etc. Let's use ^ for positional-only and $ for keyword-only as an example idea:

def my_func(^a, ^b, c, d, $e, $f):
    # a and b are now positional-only
    # c and d are whatever we want
    # e and f are keyword-only
    pass

This is way more readable in my opinion than the / and * syntax.

1 Upvotes

48 comments sorted by

10

u/socal_nerdtastic Feb 11 '25

The * seems very intuitive to me considering

def my_func(c, d, *args, e, f):

would do the same thing.

-2

u/bearinthetown Feb 11 '25

To me it's not intuitive at all that it would make e and f to be keyword-only. It could as well just force the number of parameters to be at least 4, while assigning the last two to e and f. Especially with all the advanced Python slicing syntax.

5

u/Doormatty I get paid to write Python! Feb 11 '25

Especially with all the advanced Python slicing syntax.

How does slicing play into this?

-4

u/bearinthetown Feb 11 '25

It adds expectations to how the language interpreter would behave, doesn't it? Python is very flexible when it comes to slicing, unpacking etc.

7

u/Doormatty I get paid to write Python! Feb 11 '25

I genuinely don't understand what you're trying to say here - my apologies.

I don't see how slicing factors into this at all. Unpacking yes (and I agree, it's complicated) but not slicing.

-2

u/bearinthetown Feb 11 '25

I only mean that Python makes some features more dynamic and intuitive than other languages and I see the common ground between the way it unpacks and slices. You don't need to overthink it.

5

u/socal_nerdtastic Feb 11 '25 edited Feb 11 '25

What? Do you know what *args does? Try running this:

def my_func(c, d, *args, e, f):
    print('was required:', c, d)
    print('extra data:', args)
    print('more requirements:', e, f)
    
my_func(1, 2, 3, 4, 5, 6, e=10, f=20)
my_func(1, 2, 3, 4, 5, 6, 7, 8, 9, e=10, f=20)

1

u/bearinthetown Feb 11 '25

What I mean is that it would make more sense to me if this was allowed:

def my_func(a, b, *args, c, d):
    print(a, b, args, c, d)

my_func(1, 2, 3, 4, 5, 6) # 1, 2, (3, 4), 5, 6

But it's not.

2

u/Top_Average3386 Feb 11 '25

Let's hypothetically implement your version of python, and those function definition were valid. How would you then define the behaviour of these two function calls:

  1. my_func(1,2)

  2. my_func(1,2,3,4,5,6,c=2)

1

u/bearinthetown Feb 11 '25

Both would cause errors. First one doesn't need an explanation, it's obvious that it requires at least four arguments.

As for the second one, keep in mind that even this is not allowed in Python:

def my_func(a, b, c, d):
    pass

my_func(1, 2, 3, c=5)
# TypeError: my_func() got multiple values for argument 'c'

So why would it be allowed with *args in the middle?

1

u/Top_Average3386 Feb 11 '25

Nice, the first function call was just a low ball, and won't work with the current implementation of python anyway.

But the second function call is my actual question, what I understood from your definition and example, argument after * shouldn't be keyword only but can also take its value from starargs.

Which your second example doesn't include and is how the current python works. So, how would the behaviour of


def my_func(a, b, *args, c, d):
  print(a, b, args, c, d)

my_func(1,2,3,4,5,6,c=2)

Would it:

  1. throw TypeError multiple values for c

  2. throw TypeError missing keyword argument

  3. print(1,2,(3,4,5),2,6)

1

u/Top_Average3386 Feb 11 '25

Also, a little note if you add *args in the middle it will also throw a TypeError but for a different reason.

1

u/socal_nerdtastic Feb 11 '25

Oh I see. yeah I'd agree with that. You can do it with normal unpacking but not in a function signature; that's kinda odd.

6

u/MindGoblinWhatsLigma Feb 11 '25

Idk, I personally love the (a, b, c,*args,**kwargs) syntax. I use this all the time during development.

I personally think it makes things easier to read, especially when developing an API.

3

u/bearinthetown Feb 11 '25

I'm not talking about *args and **kwargs syntax here.

5

u/MindGoblinWhatsLigma Feb 11 '25

The keyword arguments in your post are just an unpacked **kwargs. I think they're relevant.

Just like anything else, it's a tool for specific situations, and I think positional and keyword arguments (when packed) help deal with the single responsibility principle.

I can't think of a use-case to expand them out like you've done in the example, but that doesn't mean a use-case for it doesn't exist.

If **kwargs in your example is truly unneeded, you could just rearrange the arguments. It's like an order of operations thing (like there needs to be a specific order when you assign a default value to an argument)

2

u/MindGoblinWhatsLigma Feb 11 '25

Then, when you don't really care about the arguments, you can just pass in **kwargs

2

u/Pepineros Feb 11 '25

It's easy to make a feature seem overly complicated based on one overly complicated example.

The complexity here comes from having a function that requires six arguments, some of which must be keyword-only while others must be positional-only, that all have nondescript names. Not from the actual syntax of keyword vs positional itself.

2

u/bearinthetown Feb 11 '25

I don't think this is very readable neither:

def my_func(a, /, b):
    pass

2

u/Pepineros Feb 11 '25

I agree, but again it's not because of the positional-only syntax. You're defining a function that takes one positional-only argument and one keyword-or-positional argument, neither of which have default values. I cannot imagine a situation where you would want to restrict the passing of a like this. The following would work just as well and would allow any callers to decide whether they want to provide a with or without a keyword:

def my_func(a, b):
  pass

The example is unreadable because it makes no sense, not because the syntax is complicated.

1

u/bearinthetown Feb 12 '25

The biggest problem with this syntax is that you need to go back and forth to understand that the parameters are position-only, for example:

def my_func(
    some_argument,
    and_another,
    yet_another_one,
    i_like_cheese,
    my_grandma,
    okay_thats_enough,
    /,
):
    pass

You can say that it's also a strange example, but it shows the absurd of this syntax. Before you reached the last argument, you already scanned a lot of others just to learn in the end that they are actually something else than you were initially told. This is what makes programming language design bad - obscurity. And I still have no clue why would you want to force a position argument. I don't like it when the language gets bigger because it gets features nobody asked for.

1

u/fazzah PyQt | SQLAlchemy | ReportLab Feb 11 '25

Truth is you shown possible examples, but very edge cases. If I saw such code submitted to a review, I would tear it apart, unless there is some cosmic coincidence that dictates that this is the only possible way to solve an issue at hand.

The fact that python allows a LOT of code golf and various craziness, doesn't mean it's widely used in profesional settings.

1

u/bearinthetown Feb 11 '25

Then it most likely shouldn't exist I think. I like what Mark Lutz, the author of "Learning Python", which is one of, if the THE best selling Python books in history of Python, says about the path of Python's evolution. He criticizes the sh*t out of it, saying that the language has been unnecessarily polluted with mostly useless features starting from Python 3.0, making using it more difficult in real life scenarios (shared code). I agree with him to be honest.

0

u/fazzah PyQt | SQLAlchemy | ReportLab Feb 11 '25

Python is not perfect. But no language is.

But I strongly disagree with the idea that certain (optional!) features of the language should not exist, only because you either don't like or don't understand them. It's very ignorant and closeminded.

There were a few features of Python I disliked initially, but then once my knowledge of the language increased, they made more sense, in context.

1

u/bearinthetown Feb 12 '25

I know about 10 different programming languages after many years in the industry and trust me, I understand these features. What's close minded is your assumption that you can just "ignore" the problematic features. Yes, you can if you work on your project alone.

2

u/fazzah PyQt | SQLAlchemy | ReportLab Feb 12 '25

I have my share of years in the industry, worked with many teams on huge codebases. And I've seen plenty of python weirdness.  And I'm not ignoring these traits of python. To some extent we use them (or to be more precise, use the mechanisms that allow these weird traits) and it's just... normal.

Don't really see what the whole fuss is about.

1

u/MisterGerry Feb 11 '25

I'm not familiar with Python, but is it really called "keyword-only"?
What you're describing is the parameter name, not keyword.

Keywords are the words that make up the language, such as "def", "if", "while", etc.

5

u/cgoldberg Feb 11 '25

Keyword arguments ("kwargs") is common nomenclature in Python function definitions.

0

u/Doormatty I get paid to write Python! Feb 11 '25

Feel free to fork Python and make your own version.

6

u/bearinthetown Feb 11 '25

Do I need to work on something myself to be allowed to express my opinion about it?

3

u/Miserable_March_9707 Feb 11 '25

I can understand your frustration though. I've been in this game one way or another since the days of Assembly language... When "Hello World!" would take two and a half pages of code and typos like a 6 that should have been a 7 would write the output to the disk controller instead of the screen. They were the best of times. They were the worst of times.

But I digress.

As higher level languages developed, coding became easier. Languages developed and evolved. And python is doing exactly that. And of the languages that I have used over time python is truly my favorite. It does a lot very easily. But it does have complexities that from my experience are unnecessary. So does C++. And especially boring-ass old COBOL, which seems to regrow eight heads every time you try to kill it.

Oh never mind me, just an old man rambling I'll see myself out...😂😂😂😂

-6

u/Doormatty I get paid to write Python! Feb 11 '25

This is going to sound harsh, but I can't think of a better way to say it:

"Why do you think we care about your thoughts on the syntax?"

3

u/GeorgeFranklyMathnet Feb 11 '25

I don't agree with OP's thoughts, but I'm enjoying the discussion. (Maybe "sucks" was a little aggressive!)

2

u/QuasiEvil Feb 11 '25

FWIW I agree with you and like your proposal.

1

u/78yoni78 Jun 14 '25

I know that I'm late to the party, but hey, I completely agree with you!

I found this post because obviously I forgot the syntax :9

I think your syntax is pretty good. It's too bad python is weird sometimes and the syntax doesn't really make sense.

0

u/John_B_Clarke Feb 11 '25

If you're working on a project with other devs and can't keep track of the parameters, that's kind of hinting that maybe, just maybe you want to define a class.

1

u/bearinthetown Feb 11 '25

Psst... psst... class methods are also functions.

1

u/John_B_Clarke Feb 12 '25

Yes, they are, but the parameters can be defined clearly in the class definition. It may not even be necessary to pass parameters in the function call.

0

u/bearinthetown Feb 12 '25

I can tell you're new to programming.

1

u/John_B_Clarke Feb 12 '25

I can tell you're new to object oriented programming.

1

u/bearinthetown Feb 12 '25

Dude I've been programming when you were in your diapers, I suppose. Method parameters and class attributes or properties have nothing to do with each other.

1

u/John_B_Clarke Feb 12 '25

So you cut your teeth on the 7090?

1

u/[deleted] Feb 11 '25

[removed] — view removed comment

1

u/MidnightPale3220 Feb 11 '25

that only makes sense for complex arguments.

as soon as you have method calls like obj.set_title(title="new title") you'll see how ridiculous that would be.