Outils pour utilisateurs

Outils du site


issue144:tutoriel1

Ceci est une ancienne révision du document !


First, I would like to congratulate Ronnie and the entire FCM staff for 12 years of FCM! I am honoured to be a small part of this wonderful publication.

Now on to the meat of this month's article…

On April 4, 2019, PEP 570 (Python Enhancement Proposal) was accepted by the Python Steering Council, which includes Guido van Rossum. While it is unknown how soon it will be able to be implemented, the changes this brings suggest that we all should considering getting up to speed on the subject sooner than later.

Here is the link for the official page explaining the changes: https://www.python.org/dev/peps/pep-0570/#specification

and the original PEP can be found at…

https://discuss.python.org/t/pep-570-python-positional-only-parameters/1078

Note: The information presented in this article is not meant to be an in-depth teaching article on Python programming using the PEP 570 syntax. It's intended to only give you a digested overview of what to expect, once it is implemented, and to encourage you to do more research into the subject on your own.

Some background

Currently, Python uses what is called positional-or-keyword parameters. When we define a function, as we all know, we do it like this…

def my_function(parm1=None, parm2, *args, **kwds):

and for the most part, that is sufficient for most purposes. However, if someone wants to design a library, the number of parameters, placement and parameter names need to stay the same from release to release. Otherwise, it will break any software that calls it.

The new syntax for function definitions would look like that shown above.

Here are some important notes: 1) All parameters left of the / are treated as positional-only. 2) If / is not specified in the function definition, that function does not accept any positional-only arguments. 3) The logic around optional values for positional-only parameters remains the same as for positional-or-keyword parameters. 4) Once a positional-only parameter is specified with a default, the following positional-only AND positional-or-keyword parameters need to have defaults as well. 5) Positional-only parameters which do not have default values are REQUIRED positional-only parameters.

To show some of the ramifications of these changes, let's look at how we would access a function under the new requirements. Again, I borrowed the following lines from the PEP announcement…

If we have only positional_only arguments, you would use a definition like this…

def my_function(p1, p2, /):

It’s pretty much as what we are used to right now, with the exception of the “, /” characters. This satisfies the first condition above. Per condition 2 above, in this case, the “/” must be included, or it will cause a syntax error. The next example shows a positiona_only parameter set where the second parameter has a default value of ‘None’. This is consistent with condition 3 above.

def my_function(p1, p2 = None, /):

However, if the parameter for “p1” has a default value, per condition #4, ‘p2’ would also need to have a default value assigned as well. So,

def my_function(p1 = None, p2, /):

would result in a syntax error.

def my_function(p1 = None, p2 = None, /, p_or_kw):

Would also be invalid, since condition #4 above says that once a default value for a parameter is defined, all subsequent parameters, positional_only and positional_or_keyword inclusive must have a default value as well. This does not apply, however to keyword_only parameters. The following,

def my_function(p1 = None, p2 = None, /, *, kw):

would be perfectly valid and fine.

A final borrowing from the specification document is shown top right.

That’s all I have this time, so I wish you a great upcoming month.

issue144/tutoriel1.1556861583.txt.gz · Dernière modification : 2019/05/03 07:33 de d52fr