Are you taking full advantage of Python 3?

Are you sure?

Here are 10 Python 3 features that will change the way you are writing code today.

🧵👇
1⃣ Enumerations - Python 3.4+

Instead of cluttering your code with constants, you can create an enumeration using the Enum class.

An enumeration is a set of symbolic names bound to unique, constant values.

👇
2⃣ Data classes - Python 3.7+

Using data classes, Python will automatically generate special methods like "_init__" and "__repr__", reducing a lot of the clutter from your code.

This considerably reduces the amount of repetitive code that you had to write before.

👇
3⃣ Pathlib - Python 3.4+

The pathlib module provides a way to interact with the file system in a much more convenient way than dealing with os.path or the glob module.

The pathlib module makes everything simpler. When I discovered it, I've never looked back.

👇
4⃣ Type hints - Python 3.5+

You can use type hints to indicate the type of a value in your code. For example, you can use it to annotate the arguments of a function and its return type.

These hints make your code more readable, and help tools understand it better.

👇
5⃣f-strings - Python 3.6+

Instead of having to use the .format() method to print your strings, you can use f-strings for a much more convenient way to replace values in your strings.

f-strings are much more readable, concise, and easier to maintain.

👇
6⃣Extended Iterable Unpacking - Python 3.0+

Using this trick, while unpacking an iterable, you can specify a "catch-all" variable that will be assigned a list of the items not assigned to a regular variable.

Simple, but very convenient to keep the code concise.

👇
7⃣Walrus operator - Python 3.8+

Using assignment expressions (through the walrus operator :=) you can assign and return a value in the same expression.

This operator makes certain constructs more convenient and helps communicate the intent of your code more clearly.

👇
8⃣Async IO - Python 3.4+

The asyncio module is the new way to write concurrent code using the async and await syntax.

This approach allows for much more readable code and abstracts away many of the complexity inherent with concurrent programming.

👇
9⃣Underscores in Numeric Literals - Python 3.6+

This one is a small, nice addition: you can use underscores in numeric literals for improved readability.

This will shave off a few seconds every time you had to count how many digits a number had.

👇
🔟 LRU Cache - Python 3.2+

Using the functools.lru_cache decorator, you can wrap any function with a memoizing callable that implements a Least Recently Used (LRU) algorithm to evict the least recently used entries.

Do you want fast code? Look into this.

👇
Are there any specific Python 3 features that you'd like to discuss?
You can follow @svpino.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: