Manjusaka

Manjusaka

Just chatting about PEP570

Recently, I have been engrossed in the course MIT 6.824 on distributed systems and have had no motivation to write articles. However, upon seeing that PEP570 has been accepted, I have decided to write a casual article discussing PEP 570.

Python's argument#

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

As early as Python 1.0 or even earlier, Python's argument system has already supported the two main parameter forms we use today: positional and keyword. Let's look at 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 usage patterns?

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

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

Suppose we have a function definition like this:

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

Then this function can only be called in a few 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)

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

A Casual Discussion on 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 ensures that the function can only be called 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 effect 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 degree of support for syntax features. And both PEP 570 and PEP 3102 solve this problem.

So from my perspective, I think PEP 570 is quite an important and meaningful proposal (both are PEP57x, 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 getting more and more casual in my writing...

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