|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Python 3.7 feature walkthrough" |
| 4 | +date: "2019-01-13 01:23:12 +0530" |
| 5 | +tag: |
| 6 | + - Python |
| 7 | + - CorePython |
| 8 | + - Python3.7 |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +In this post, I will explain improvements done in Core Python version 3.7. Below |
| 13 | +is the outline of features covered in this post. |
| 14 | + |
| 15 | +* Breakpoints |
| 16 | + |
| 17 | +* Subprocess |
| 18 | + |
| 19 | +* Dataclass |
| 20 | + |
| 21 | +* Namedtuples |
| 22 | + |
| 23 | +* Hash-based Python object file |
| 24 | + |
| 25 | +### breakpoint() |
| 26 | + |
| 27 | +Breakpoint is an extremely important tool for debugging. Since I started |
| 28 | +learning Python, I am using the same API for putting breakpoints. With this |
| 29 | +release, ```breakpoint()``` is introduced as a built-in function. Because it is |
| 30 | +in a built-in scope, you don't have to import it from any module. You can call |
| 31 | +this function to put breakpoints in your code. This approach is handier than |
| 32 | +importing ```pdf.set_trace()```. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +Code used in above example |
| 37 | + |
| 38 | +```python |
| 39 | +for i in range(100): |
| 40 | + if i == 10: |
| 41 | + breakpoint() |
| 42 | + else: |
| 43 | + print(i) |
| 44 | + |
| 45 | +``` |
| 46 | + |
| 47 | +### PYTHONBREAKPOINT |
| 48 | + |
| 49 | +There wasn't any handy option to disable or enable existing breakpoints with a |
| 50 | +single flag. But with this release, you can certainly reduce your pain by using |
| 51 | +```PYTHONBREAKPOINT``` environment variable. You can disable all breakpoints in |
| 52 | +your code by setting the environment variable ```PYTHONBREAKPOINT``` to ```0```. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +##### I advise putting "PYTHONBREAKPOINT=0" in your production environment to avoid unwanted pausing at forgotten breakpoints |
| 58 | + |
| 59 | + |
| 60 | +### Subprocess.run(capture_output=True) |
| 61 | + |
| 62 | +You can pipe the output of Standard Output Stream (stdout) and Standard Error |
| 63 | +Stream (stderr) by enabling ```capture_output``` parameter of |
| 64 | +```subprocess.run()``` function. |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +You should note that it is an improvement over piping the stream manually. For |
| 69 | +example, ```subprocess.run(["ls", "-l", "/var"], stdout=subprocess.PIPE, |
| 70 | +stderr=subprocess.PIPE)``` was the previous approach to capture the output of |
| 71 | +```stdout``` and ```stderr```. |
| 72 | + |
| 73 | + |
| 74 | +### Dataclasses |
| 75 | + |
| 76 | +The new class level decorator ```@dataclass``` introduced with the |
| 77 | +```dataclasses``` module. Python is well-known for achieving more by writing |
| 78 | +less. It seems that this module will receive more updates in future which can be |
| 79 | +applied to reduce significant line of code. Basic understanding of Typehints is |
| 80 | +expected to understand this feature. |
| 81 | + |
| 82 | +When you wrap your class with the ```@dataclass``` decorator, the decorator will |
| 83 | +put obvious constructor code for you. Additionally, it defines a behaviour for |
| 84 | +dander methods ```__repr__()```, ```__eq__()``` and ```__hash__()```. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +Below is the code before introducing a ```dataclasses.dataclass``` decorator. |
| 90 | + |
| 91 | +```python |
| 92 | +class Point: |
| 93 | + |
| 94 | + def __init__(self, x, y): |
| 95 | + self.x = x |
| 96 | + self.y = y |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | +After wrapping with ```@dataclass``` decorator it reduces to below code |
| 101 | + |
| 102 | +```python |
| 103 | +from dataclasses import dataclass |
| 104 | + |
| 105 | + |
| 106 | +@dataclass |
| 107 | +class Point: |
| 108 | + x: float |
| 109 | + y: float |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +### Namedtuples |
| 114 | + |
| 115 | +The namedtuples are a very helpful data structure, yet I found it is less known |
| 116 | +amongst developers. With this release, you can set default values to argument |
| 117 | +variables. |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +##### Note: Default arguments will be assigned from left to right. In the above example, default value ``2`` will be assigned to variable ``y`` |
| 122 | + |
| 123 | +Below is the code used in the example |
| 124 | + |
| 125 | +```python |
| 126 | +from collections import namedtuple |
| 127 | + |
| 128 | + |
| 129 | +Point = namedtuple("Point", ["x", "y"], defaults=[2,]) |
| 130 | +p = Point(1) |
| 131 | +print(p) |
| 132 | +``` |
| 133 | + |
| 134 | + |
| 135 | +### .pyc |
| 136 | + |
| 137 | +**.pyc** are object files generated everytime you change your code file (.py). |
| 138 | +It is a collection of meta-data created by an interpreter for an executed code. |
| 139 | +The interpreter will use this data when you re-execute this code next time. |
| 140 | +Present approach to identify an outdated object file is done by comparing meta |
| 141 | +fields of source code file like last edited date. With this release, that |
| 142 | +identification process is improved by comparing files using a hash-based |
| 143 | +approach. The hash-based approach is quick and consistent across various |
| 144 | +platforms than comparing last edited dates. This improvement is considered |
| 145 | +unstable. Core python will continue with the metadata approach and slowly |
| 146 | +migrate to the hash-based approach. |
| 147 | + |
| 148 | + |
| 149 | +### Summary |
| 150 | + |
| 151 | +* Calling ```breakpoint()``` will put a breakpoint in your code. |
| 152 | + |
| 153 | +* Disable all breakpoints in your code by setting an environment variable |
| 154 | + ```PYTHONBREAKPOINT=0```. |
| 155 | + |
| 156 | +* ```subprocess.run([...], capture_output=True)``` will capture the output of |
| 157 | + ```stdout``` and ```stderr```. |
| 158 | + |
| 159 | +* Class level decorator ```@dataclass``` will define default logic for |
| 160 | + constructor function. It will implement default logic for dunder methods |
| 161 | + ```__repr__()```, ```___eq__()``` and ```__hash__()```. |
| 162 | + |
| 163 | +* Namedtuple data structure supports default values to its arguments using |
| 164 | + ```defaults```. |
| 165 | + |
| 166 | +* Outdated Python object files (.pyc) are compared using the hash-based |
| 167 | + approach. |
| 168 | + |
| 169 | + |
| 170 | +I hope you were able to learn something new by reading this post. If you want to |
| 171 | +read an in-depth discussion on each feature introduced in Python 3.7, then |
| 172 | +please read |
| 173 | +[this](https://docs.python.org/3.7/whatsnew/changelog.html#python-3-7-0-final) |
| 174 | +official post. Happy hacking! |
| 175 | + |
| 176 | + |
| 177 | +###### Proofreaders: [Jason Braganza](https://janusworx.com/), Ninpo, basen_ from #python at Freenode, Ultron from #python-offtopic at Freenode, up|ime from ##English at Freenode |
0 commit comments