|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Walkthrough with features introduced in Core Python 3.7" |
| 4 | +date: "2018-08-12 15:51:15 +0530" |
| 5 | +tag: |
| 6 | + - Python |
| 7 | + - CorePython |
| 8 | + - Python3.7 |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +In this post, I will try to explain improvements done in Core Python version |
| 13 | +3.7. Below is the summary of features covered in this post. |
| 14 | + |
| 15 | +* Breakpoints |
| 16 | + |
| 17 | +* Subprocess |
| 18 | + |
| 19 | +* Dataclass |
| 20 | + |
| 21 | +* Int with underscores |
| 22 | + |
| 23 | +* Namedtuples |
| 24 | + |
| 25 | +* Hash-based Python object file |
| 26 | + |
| 27 | +### breakpoint() |
| 28 | + |
| 29 | +Breakpoints are extream important tool for debugging. Since I started learning |
| 30 | +Python, I am using the same API for putting breakpoints. With this release, |
| 31 | +breakpoint() is introduced as n new built-in function which can be used for |
| 32 | +putting breakpoints in your code. Putting breakpoint by calling a breakpoint |
| 33 | +function is handier than importing a ```pdf.set_trace()```. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Code used in above example |
| 38 | + |
| 39 | +```python |
| 40 | +for i in range(100): |
| 41 | + if i == 10: |
| 42 | + breakpoint() |
| 43 | + else: |
| 44 | + print(i) |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | +### PYTHONBREAKPOINT |
| 49 | + |
| 50 | +There wasn't any handy option to |
| 51 | +disable or enable existing breakpoints with single flag. But with this release |
| 52 | +you can certainly reduce your pain by using ```PYTHONBREAKPOINT``` environment |
| 53 | +variable. You can disable all breakpoints in your code by setting the environment variable |
| 54 | +```PYTHONBREAKPOINT``` to ```0```. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +##### Note Use "PYTHONBREAKPOINT=0" in your production environment to avoid unwanted pausing at forgotten breakpoints |
| 60 | + |
| 61 | + |
| 62 | +### Subprocess.run(capture_output=True) |
| 63 | + |
| 64 | +You can pipe the output of Standard Output Stream (stdout) and Standard Error |
| 65 | +Stream (stderr) by enabling ```capture_output``` parameter of |
| 66 | +```subprocess.run()``` function. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +### Dataclasses |
| 72 | + |
| 73 | +The new class level decorator ```@dataclass``` introduced with ```dataclasses``` |
| 74 | +module. It will reduce many lines of your code. Python is well-known for |
| 75 | +developing features which allows to achieving more by writing less. Basic |
| 76 | +understanding of Typehints is expected to understand this feature. |
| 77 | + |
| 78 | +The ```@dataclass``` decorator will put obvious construct code. Additionally, it |
| 79 | +will define a behaviour for dander methods ```__repr__()```, ```__eq__()``` and |
| 80 | +```__hash__()``` for us. |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +Below is the code before introducing a ```dataclasses.dataclass``` decorator. |
| 86 | + |
| 87 | +```python |
| 88 | +class Point: |
| 89 | + |
| 90 | + def __init__(self, x, y): |
| 91 | + self.x = x |
| 92 | + self.y = y |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +After wrapping with ```@dataclass``` decorator it reduces to below code |
| 97 | + |
| 98 | +```python |
| 99 | +from dataclasses import dataclass |
| 100 | + |
| 101 | + |
| 102 | +@dataclass |
| 103 | +class Point: |
| 104 | + x: float |
| 105 | + y: float |
| 106 | +``` |
| 107 | + |
| 108 | +### Putting underscores in defining Integers: |
| 109 | + |
| 110 | +It is difficult to read when a variable is having a large decimal value. Many |
| 111 | +times I myself put finger on the screen and count the places to get an idea. In |
| 112 | +this version, you can put ```_``` for separating numbers for constructing more |
| 113 | +readable value. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +Below is the code used in the example |
| 118 | + |
| 119 | +```python |
| 120 | +x = 1_00_00_00 |
| 121 | +print(x) |
| 122 | +``` |
| 123 | + |
| 124 | +### Namedtuples |
| 125 | + |
| 126 | +According to me namedtuples are less known, but very helpful feature in Python. |
| 127 | +With this release, you can define default arguments of variables. |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +##### Note: Default arguments will be assigned from left to right. In the above example, default value ``2`` will be assigned to variable ``y`` |
| 132 | + |
| 133 | +Below is the code used in the example |
| 134 | + |
| 135 | +```python |
| 136 | +from collections import namedtuple |
| 137 | + |
| 138 | + |
| 139 | +Point = namedtuple("Point", ["x", "y"], defaults=[2,]) |
| 140 | +p = Point(1) |
| 141 | +print(p) |
| 142 | +``` |
| 143 | + |
| 144 | +### .pyc |
| 145 | + |
| 146 | +***.pyc*** are object files generated every time you change your Python code |
| 147 | +file (.py). At present identifying the change in Python code is done by |
| 148 | +comparing meta fields like last edited date and by few other fields. With this |
| 149 | +release, that functionality is improved by comparing files with a hash-based |
| 150 | +approach. Hashed approach is quick and more consistent than a metadata approach. |
| 151 | +Though this improvement is still considered unstable and CPython will continue |
| 152 | +with the metadata approach. |
0 commit comments