Manjusaka

Manjusaka

Just chatting about PEP570

Recently, I have been obsessed with the course MIT 6.824 on distributed systems and have had no motivation to write articles. However, when I saw that PEP570 was accepted, I decided to write a casual article to discuss PEP 570.

Python's arguments#

Before discussing PEP570, let's take a look at the evolution of Python's argument system.

As early as Python 1.0 or earlier, Python's argument system already supported the two main types of parameters that we currently use: positional and keyword. Here are a few examples:


def abc(a, b, c):
    pass


abc(1, 2, 3)
abc(1, 2, c=3)
abc(1, b=2, c=3)
abc(*(1, 2, 3))
abc(**{"a": 1, "b": 2, "c": 3})

Are these not the most common ways we use arguments?

After a long period of development, although there were some proposals to optimize and enhance Python's argument system, they were consistently rejected until the introduction of PEP3102.

PEP 3102 mainly introduced a concept called Keyword-Only Arguments. Here's an example:

Suppose we have a function definition like this:

def abc(a, *, b, c):
    pass

Then this function can only be called in the following ways:

def abc(a, *, b, c):
    pass


abc(**{"a": 1, "b": 2, "c": 3})
abc(a=1, b=2, c=3)
abc(1, b=2, c=3)

Okay, now that we have briefly discussed the iterative process of arguments, let's talk about PEP 570.

Let's Talk About PEP 570#

PEP 570 does something similar to PEP 3102. While PEP 3102 introduced syntax sugar to support keyword-only usage of functions, PEP 570 allows functions to support positional-only usage.

Suppose we have a function definition like this:

def abc(a, b, /, c):
    pass

PEP 570 allows the function to be called only in the following ways:

def abc(a, b, /, c):
    pass


abc(1, 2, c=3)
abc(1, 2, 3)

What would happen if we didn't do this? Let's try it out. Currently, PEP 570 has an implementation, which can be seen in bpo-36540: PEP 570 -- Implementation. Let's compile and test it, and the result is as follows:

image

Random Thoughts#

Many people haven't really thought about the significance of PEP 570, and PEP 570 also mentions many motivations. However, I personally think that it, like PEP 3102, is practicing a principle, which is:

explicit is better than implicit

In other words, if we want to ensure the consistency of code style as much as possible, we need some level of support for syntax features. And PEP 570 and PEP 3102 solve this kind of problem.

So from my perspective, I think PEP 570 is quite an important and meaningful proposal (both PEP 57x, why is there such a difference in treatment? (laughs).

By the way, let me tell you a joke. Serhiy Storchaka, one of Python's core developers, really likes this PEP. Before the implementation of PEP 570 was merged into the main branch, he had already made some improvements to some built-in libraries. You can go and take a look at the PR [WIP] Use PEP 570 syntax for positional-only parameters.

Alright, that's the end of today's casual article... I'm really becoming more and more casual in my writing...

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.