Thursday, February 18, 2021

My wish for Python 4

I love Python. 

A few years after I started using it, I saw someone writing about it and using the phrase "it fits my brain": this is very much how I feel ... at least, for simple straightforward code that doesn't rely on weird metaclass constructs, or even with the added distraction of type annotations. [Yes, type annotations can be extremely useful, but they do not (currently) "fit my brain".]

I am extremely grateful to the many volunteers that work constantly to improve Python. Thanks to their efforts, Python keeps growing. I see most of the growth as positives: more users, more applications in a growing number of fields. In many universities, Python has displaced languages such as Scheme and (thankfully!) Java as the first language that students learn.  From *my* limited point of view, there is a small negative in that this growth of Python includes a growth in its syntax: when I first encountered Python (version 2.3), it had a comparatively very simple syntax which meant that it was easier to learn (in spite of some warts that were fixed in the 2 to 3 transition) compared with the latest Python version (3.10). 

I am well aware that Python doesn't use semantic version numbers: code written for version 3.x can be incompatible with code written for version 3.y. [This was also the case for the 2.x series.] As a decision has been made to use two-digit minor version numbers, there is no apparent need to think of a version 4 of Python: improvements can continue for many years while keeping 3 as the main version.  However, I wish there could be a version 4 - as I describe below.

Note that when I think of Python, I do not think of a specific implementation (such as CPython), but I think of Python as the language. I do not consider "implementation details" such as the Global Interpreter Lock (GIL) as part of the language. Yes, it would be useful if the main Python implementation could make better use of multi-core CPUs. However, that is not something that is relevant for the purpose of this post.

So, what is my wish for Python 4? ...  

Above all, going from the last 3.x version (let's call it 3.14...) to 4.0 should be done seamlessly: code written for version 3.14 should run as is in Python 4.0.

I would like for Python 4 to get inspired by Racket and introduce "dialects".  Python could even borrow the notation used by Racket (#lang dialect) as a top directive in a given module to specify the dialect used in that module.  Unlike Racket, I would limit the number of possible dialects to 4.

The main dialect would not need to be specified: it would simply be the standard Python that everyone knows and loves (or not). It would continue evolving, changing slightly as it goes from version 4.x to 4.y.

A second dialect would be an "experimental" dialect. This dialect could be use to introduce some new syntax with no guarantee whatsoever of backward (or forward) compatibility. It would allow people to experiment with proposed new syntactic constructs before deciding to incorporate them (or not) in the main dialect. I honestly think that this would help reduce some friction in the Python community as changes are proposed and adopted.  The main benefit of such a dialect would be more social than technical.

A third dialect would be a "beginner" dialect. The goal of the beginner dialect would be to make it easier to learn basic programming concepts as opposed to learning a quirky syntax to express these concepts. This beginner dialect would be, in version 4.0, a strict subset of the main dialect. It would not include type annotations, and it might perhaps also exclude the new pattern matching syntax and other syntactic constructs.  For example, using the keyword is might only be limited to checking if the object is one of the three singletons (None, True, False); other uses of is could and should be done with "== id(thing)". Based on feedback from educators, it might perhaps make sense to eventually introduce a few additional keywords and constructs not available from the main dialect, such as:

  • Having nobreak as a keyword equivalent to else in loops.
  • Having function as a keyword equivalent to lambda.
  • Having imported as a keyword with a meaning equivalent to not __name__ == "__main__".
  • Having repeat as a keyword  as is available in TygerJython and Reeborg's World for the construct repeat nb_steps:. It might perhaps be also useful to have repeat forever: as equivalent to Python's while True: . [Other possible uses of this keyword are described here.]
Finally, a fourth dialect would be a "static" dialect. This fourth dialect would always be using a syntax strictly compatible with the main dialect. In this dialect, some dynamical features of Python (such as the possibility to change the type of the object specified by a given name) would not be available so that some optimizations could be applied to increase the execution speed.  I am sure that experts would be able to suggest other restrictions that could be used to greatly increase the execution speed.  I think that such a dialect would be one that would generate the most enthusiastic response from Python users.

That being said, I doubt very much that I'll ever see Python adopting these ideas. However, it is sometimes nice to dream ...

No comments: