On February 28th 2018, the second beta of Python 3.7 was released. This new version contains lots of fixes and, notably, several new features available for everyone to test. The pre-release of Python 3.7 is available not only in Fedora Rawhide but also all other Fedora versions. Read more about it below.
Installation and basics
It’s easy to install Python 3.7 on supported Fedora. Run this command:
sudo dnf install python37
Then, run the command python3.7 to test things out. You can also create a virtual environment using the new version, or add py37 to your project’s tox.ini and start testing on the freshest Python available:
$ python3.7 -m venv venv $ . venv/bin/activate (venv) $ python --version Python 3.7.0b2
There are no extra libraries or software for Python 3.7 packaged in Fedora yet. However, the whole ecosystem is available through virtualenv and pip:
(venv) $ python -m pip install requests # or any other package Collecting requests Using cached requests-2.18.4-py2.py3-none-any.whl Collecting idna<2.7,>=2.5 (from requests) Using cached idna-2.6-py2.py3-none-any.whl Collecting certifi>=2017.4.17 (from requests) Using cached certifi-2018.1.18-py2.py3-none-any.whl Collecting urllib3<1.23,>=1.21.1 (from requests) Using cached urllib3-1.22-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 (from requests) Using cached chardet-3.0.4-py2.py3-none-any.whl Installing collected packages: idna, certifi, urllib3, chardet, requests Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
New Python 3.7 feature: Data classes
Here’s an example of a killer new feature in Python 3.7.
How many times have you written out self.attribute = attribute in your __init__ method? For most Python devs, the answer is “a lot.” Combined with __repr__ and support for comparisons, there’s a lot of boilerplate involved in creating a class that only holds a bunch of data. The excellent attrs project solves many of these issues. Now a carefully selected subset of the ideas in attrs is making its way to the standard library.
Here’s an example of the feature in action:
from dataclasses import dataclass @dataclass class Point: x: float y: float z: float = 0.0 >>> p = Point(1.5, 2.5) >>> print(p) Point(x=1.5, y=2.5, z=0.0)
Note the types are just documentation. They aren’t actually checked at runtime, though they will work with static checkers like mypy. Data classes are documented in PEP 557 for now. If the API feels too limited, remember the small scope is intentional. You can always switch to using the full attrs library if you need more features.
This and other new features come with the release. Python 3.7 will become the default Python version in Fedora 29. If you spot any bugs, please report them in Fedora Bugzilla or directly upstream. If you have any questions, ask the Python SIG at email@example.com or visit IRC Freenode channel #fedora-python.