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.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread">
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
Are you sure?
Here are 10 Python 3 features that will change the way you are writing code today.
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.
More information about enumerations:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://docs.python.org/3.4/library/enum.html
https://docs.python.org/3.4/libra... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://pythonspot.com/python-enum/
https://pythonspot.com/python-en... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about data classes:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://realpython.com/python-data-classes/
https://realpython.com/python-da... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://docs.python.org/3/library/dataclasses.html
https://docs.python.org/3/library... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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& #39;ve never looked back.
More information about the pathlib module:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://realpython.com/python-pathlib/
https://realpython.com/python-pa... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://docs.python.org/3/library/pathlib.html
https://docs.python.org/3/library... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about type hints:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> http://veekaybee.github.io/2019/07/08/python-type-hints/
https://veekaybee.github.io/2019/07/0... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://docs.python.org/3/library/typing.html
https://docs.python.org/3/library... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about f-strings:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://realpython.com/python-f-strings/
https://realpython.com/python-f-... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://www.python.org/dev/peps/pep-0498/
https://www.python.org/dev/peps/... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about Extended Iterable Unpacking:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://www.python.org/dev/peps/pep-3132/
https://www.python.org/dev/peps/... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://www.rfk.id.au/blog/entry/extended-iterable-unpacking/
https://www.rfk.id.au/blog/entr... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about the Walrus operator:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://deepsource.io/blog/python-walrus-operator/
https://deepsource.io/blog/pyth... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://www.python.org/dev/peps/pep-0572/
https://www.python.org/dev/peps/... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about the asyncio module:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://realpython.com/async-io-python/
https://realpython.com/async-io-... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://docs.python.org/3/library/asyncio.html
https://docs.python.org/3/library... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about underscores in numeric literals:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://www.python.org/dev/peps/pep-0515/
https://www.python.org/dev/peps/... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
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.
More information about the lru_cache decorator:
https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> #functools.lru_cache">https://docs.python.org/3/library/functools.html #functools.lru_cache
https://docs.python.org/3/library... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat"> https://www.cameronmacleod.com/blog/python-lru-cache
https://www.cameronmacleod.com/blog/pyth... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
Are there any specific Python 3 features that you& #39;d like to discuss?