Is Python 3.12 Worth The Hype?

13 October 2023



Python 3.12 is coming out with type aliases, a override decorator, performance improvements, new builtins and nested f-strings. Wow a lot of features to cover which are worth the hype?







Type Aliases

Yes you can already make these but now there is a special syntax for them with the type keyword.

type Point = tuple[float, float]

You can see that we are making a point type which is a tuple of two floats which is equivalent to this.

annotation-def VALUE_OF_Point(): return tuple[float, float] Point = typing.TypeAliasType("Point", VALUE_OF_Point())

Annotation scopes are like functions but with a few small differences. So you might be asking how is this any different then just doing this.

Point = tuple[float, float]

Well that is a typing.GenericAlias while the type statement makes a typing.TypeAliasType, what the hell is the difference between those two. Well to answer your question, this will make type checking with mypy for example easier.

Mypy will treat Point the same as tuple[float, float] literally so passing a tuple of two floats will be treated as Point and passing an instance of Point will also work.







Override Decorator

Let's say you have a Base class and you want to create an Extended class from the Base class, you want to overwrite the name function to return "Extended" instead of "Base".

You misspelled name to nam and the previous function has not been overwrited, in a large codebase you might not notice this and when testing through a function that uses another function that uses a third function that uses that class you will be pretty confused.

So you can use the override decorator which will inform you if your overwriting a existing function or not.

@override def name(self): return "Extended"

Works! How about we misspell it though.

@override def nam(self): return "Extended"

Oh no! It throws an error so we can catch and debug back to this specific function instead of being confused by a logical bug.







Performance Improvements

Python 3.12 is much more performant then Python 3.11.

I cannot go in-depth here but this this article is great to learn more.

Seeing a dynamically-typed language being faster says a lot about the future of programming.







Nested f-strings

Before you would need to do this crazy stuff to nest f-strings.

f"""{f'''{f"{f''}"}'''}"""

Now you can just do this.

f"{f"{f"{f""}"}"}"







So indeed yes, Python 3.12 is worth the hype and future Python versions will bring even more improvements such as Python 3.14 could be faster then C++ but that's a story for another time.