10
10
11
11
12
12
In this post, I will try to explain improvements done in Core Python version
13
- 3.7. Below is the summary of features convered in this post.
13
+ 3.7. Below is the summary of features covered in this post.
14
14
15
15
* Breakpoints
16
16
@@ -22,187 +22,179 @@ In this post, I will try to explain improvements done in Core Python version
22
22
23
23
* Namedtuples
24
24
25
- * Hashbased Python object file
26
-
25
+ * Hash-based Python object file
27
26
28
27
* Breakpoint:
29
28
30
- * breakpoint():
29
+ * breakpoint():
31
30
32
- Breakpoints are extream important for debugging. Since I started learning
33
- Python, I am using the same API of putting breakpoints. With this release,
34
- breakpoint() is a new build -in function for putting breakpoints in your
35
- code. This way is more handy than its previous way which is importing and
36
- calling a set_trace method from the pdb module like pdb.set_trace
31
+ Breakpoints are extream important for debugging. Since I started learning
32
+ Python, I am using the same API for putting breakpoints. With this release,
33
+ breakpoint() is a new built -in function for putting breakpoints in your code.
34
+ This way is handier than its previous way which is importing and calling a
35
+ set_trace method from the pdb module like pdb.set_trace
37
36
38
- Below is the example of using putting breakpoint in your program:
37
+ Below is the example of putting a breakpoint in your program:
39
38
40
- # Note: A Gif will be more batter example for this.
41
- ``` python
42
- >> > for i in range (5 ):
43
- ... if i % 2 == 0 :
44
- ... breakpoint ()
45
- ... else :
46
- ... print (i)
47
- ...
48
- ```
39
+ # Note: A Gif will be more batter example for this.
49
40
50
- Explaination:
41
+ '''python
42
+ > ;> ;> ; for i in range(5):
43
+ ... if i % 2 == 0:
44
+ ... breakpoint()
45
+ ... else:
46
+ ... print(i)
47
+ ...
48
+ '''
51
49
52
- The above code will take a pause by opening a Python Debugger (pdb) when the
53
- value of iterator variable i will be even. All the odd values are printed on
54
- screen.
50
+ Explanation:
55
51
56
- * PYTHONBREAKPOINT environment variable:
52
+ The above code will take a pause by opening a Python Debugger (pdb) when the
53
+ value of iterator variable i is even. All the odd values of it are printed on
54
+ the screen.
57
55
58
- You can disable all the breakpoints in your code by setting this environment
59
- variable to " 1" . Sometimes debugging can lead to a level where number of
60
- breakpoints are out of control. Process of finding, removing and re- ading a
61
- breakpoint becomes overhead. This environment variable can provide a great
62
- way of toggling them in one shot.
56
+ * PYTHONBREAKPOINT environment variable:
63
57
64
- # TODO : Add demonstration of gif for this point.
58
+ You can disable all the breakpoints in your code by setting this environment
59
+ variable to “1”. Sometimes debugging can lead to a level where a number of
60
+ breakpoints are out of control. The process of finding, removing and reading
61
+ breakpoints becomes an overhead. This environment variable can provide a great
62
+ way of toggling them in one shot.
65
63
66
- I advise to set " PYTHONBREAKPOINT=0" at production environment. This will
67
- prevent your code from long holding if you merge a code with breakpoint by
68
- mistake.
64
+ # TODO: Add demonstration of GIF for this point.
69
65
66
+ I advise setting "PYTHONBREAKPOINT=0" in the production environment. This will
67
+ prevent your code from long holding if you merge a code with breakpoint by
68
+ mistake.
70
69
71
70
* Subprocess got new parameter capture_output:
72
71
73
- You can pipe the output of Standard Output stream (stdout) and Standard Error
74
- stream (stderr) by enabling capture_output parameter of subprocess.run
75
- command.
76
-
77
- Without this parameter, capturing the Standard streams was lengthy. Below is
78
- the example by using subprocess.PIPE parameter
79
-
80
- ```python
81
- subprocess.run(
82
- [" ls" , " -l" , " /var" ], stdout = subprocess.PIPE , stderr = subprocess.PIPE
83
- )
84
- ```
85
-
86
- With capture_output parameter, this will become more concise like below
87
-
88
- ``` python
89
- subprocess.run([" ls" , " -l" , " /var" ], capture_output = True )
90
- ```
91
-
92
- Below is the detailed example of using captured_true parameter with
93
- subprocess.run command.
94
-
95
- ``` python
96
- >> > import subprocess
97
- >> > ls = subprocess.run([" ls" , " -l" , " /var" ], capture_output = True )
98
- >> > print (ls.stdout.decode())
99
- total 28
100
- drwxr- xr- x 11 root root 4096 Apr 24 15 :14 cache
101
- drwxr- xr- x 4 root root 4096 Aug 12 15 :28 db
102
- drwxr- xr- x 2 root root 4096 Jul 2 13 :47 empty
103
- drwxr- xr- x 34 root root 4096 Jul 20 04 :22 lib
104
- lrwxrwxrwx 1 root root 9 Oct 12 2017 lock -> / run/ lock
105
- drwxr- xr- x 11 root root 4096 Aug 12 14 :52 log
106
- lrwxrwxrwx 1 root root 15 Apr 23 14 :08 mail -> / var/ spool/ mail
107
- lrwxrwxrwx 1 root root 4 Oct 12 2017 run -> / run
108
- drwxr- xr- x 7 root root 4096 Jun 15 09 :22 spool
109
- drwxrwxrwt 3 root root 4096 Aug 10 15 :15 tmp
110
-
111
- >> > print (ls.stderr.decode())
112
-
72
+ You can pipe the output of Standard Output stream (stdout) and Standard Error
73
+ stream (stderr) by enabling capture_output parameter of subprocess.run
74
+ command.
75
+
76
+ Without this parameter, capturing the Standard streams was lengthy. Below is the example by using subprocess.PIPE parameter
77
+
78
+ '''python
79
+ subprocess.run(
80
+ [ "ls", "-l", "/var"] , stdout=subprocess.PIPE, stderr=subprocess.PIPE
81
+ )
82
+ '''
83
+
84
+ With capture_output parameter, this will become more concise like below
85
+
86
+ '''python
87
+ subprocess.run([ "ls", "-l", "/var"] , capture_output=True)
88
+ '''
89
+
90
+ Below is the detailed example of using captured_true parameter with
91
+ subprocess.run command.
92
+
93
+ '''python
94
+ > ;> ;> ; import subprocess
95
+ > ;> ;> ; ls = subprocess.run([ "ls", "-l", "/var"] , capture_output=True)
96
+ > ;> ;> ; print(ls.stdout.decode())
97
+ total 28
98
+ drwxr-xr-x 11 root root 4096 Apr 24 15:14 cache
99
+ drwxr-xr-x 4 root root 4096 Aug 12 15:28 db
100
+ drwxr-xr-x 2 root root 4096 Jul 2 13:47 empty
101
+ drwxr-xr-x 34 root root 4096 Jul 20 04:22 lib
102
+ lrwxrwxrwx 1 root root 9 Oct 12 2017 lock -> ; /run/lock
103
+ drwxr-xr-x 11 root root 4096 Aug 12 14:52 log
104
+ lrwxrwxrwx 1 root root 15 Apr 23 14:08 mail -> ; /var/spool/mail
105
+ lrwxrwxrwx 1 root root 4 Oct 12 2017 run -> ; /run
106
+ drwxr-xr-x 7 root root 4096 Jun 15 09:22 spool
107
+ drwxrwxrwt 3 root root 4096 Aug 10 15:15 tmp
108
+
109
+ > ;> ;> ; print(ls.stderr.decode())
113
110
114
111
* Dataclasses module:
115
112
116
- The new class level decorator " @dataclass" will reduce many lines of your
117
- code. Python is well known for developing features which allows to achieve
118
- more by writing less. For understanding this feature batter, you should be
119
- aware of type hints. I will try to write a tutorial on type hints in up coming
120
- days. Until there, I will suggest to grab any available tutorial from your
121
- Google search.
122
-
123
- Below is the usual way to define a Class and creating an object from it. I
124
- will explain how below approach can be reduced by wrapping a @ dataclass
125
- decorator to it.
126
-
127
- ```python
128
- >> > class Point :
129
- ... def __init__ (self , x , y ):
130
- ... self .x = x
131
- ... self .y = y
132
- ...
133
- >> > p = Point(2 , 4 )
134
- >> > p
135
- < __main__.Point object at 0x 7f3c73688b00>
136
- ```
137
-
138
- Converting above example with @dataclass decorator, Those lines of putting
139
- odious construct code is gone! Additionally, the class level decorator
140
- @dataclass will automatically define a behaviour for dander methods
141
- __ repr__ (), __ eq__ () and __ hash__ () for us.
142
-
143
- Below is the example code of wrapping Point class with @dataclass decorator
144
- demonstrated earlier.
145
-
146
- ``` python
147
- >> > from dataclasses import dataclass
148
- >> > @ dataclass
149
- ... class Point :
150
- ... x: float
151
- ... y: float
152
- ...
153
- >> > p = Point(2 , 4 )
154
- >> > p
155
- Point(x = 2 , y = 4 )
156
- ```
157
-
113
+ The new class level decorator "@dataclass " will reduce many lines of your code.
114
+ Python is well-known for developing features which allow achieving more by
115
+ writing less. For understanding this feature batter, you should be aware of type
116
+ hints. I will try to write a tutorial on type hints in upcoming days. Until
117
+ there, I will suggest grabbing any available tutorial from Google.
118
+
119
+ Below is the usual way to define a Class and creating an object from it. I will
120
+ explain how below approach can be reduced by wrapping a @dataclass decorator to
121
+ it.
122
+
123
+ '''python
124
+ > ;> ;> ; class Point:
125
+ ... def __ init__ (self, x, y):
126
+ ... self.x = x
127
+ ... self.y = y
128
+ ...
129
+ > ;> ;> ; p = Point(2, 4)
130
+ > ;> ;> ; p
131
+ <__ main__ .Point object at 0x7f3c73688b00>
132
+ '''
133
+
134
+ Converting above example with @dataclass decorator, Those lines of putting
135
+ obvious construct code will gone! Additionally, it will define a behaviour for
136
+ dander methods __ repr__ (), __ eq__ () and __ hash__ () for us.
137
+
138
+ Below is the example code of wrapping Point class demonstrated earlier with
139
+ @dataclass decorator.
140
+
141
+ '''python
142
+ > ;> ;> ; from dataclasses import dataclass
143
+ > ;> ;> ; @dataclass
144
+ ... class Point:
145
+ ... x: float
146
+ ... y: float
147
+ ...
148
+ > ;> ;> ; p = Point(2, 4)
149
+ > ;> ;> ; p
150
+ Point(x=2, y=4)
151
+ '''
158
152
159
153
* Putting underscores in defining Integers:
160
154
161
- Writing large numbers as value invites a difficulty of reading it later. Many
162
- times I myself put finger on screen and count the places to get an idea of
163
- number. In this version, you can put _ for separating numbers for constructing
164
- more readable value. Below example will make more understanding to this.
165
-
166
- ``` python
167
- >> > x = 1_00_000
168
- >> > x
169
- 100000
170
- ```
155
+ Writing large numbers as value invites a difficulty of reading it later. Many
156
+ times I myself put finger on the screen and count the places to get an idea of
157
+ number. In this version, you can put _ for separating numbers for constructing
158
+ more readable value. Below example will make more understanding of this.
171
159
172
- Explanation:
160
+ '''python
161
+ > ;> ;> ; x = 1_00_000
162
+ > ;> ;> ; x
163
+ 100000
164
+ '''
173
165
174
- The integer value of x is 1, 00, 000. Putting underscore at places of comma
175
- makes larger value more readable than writing entire value together.
166
+ Explanation:
176
167
168
+ The integer value of x is 1, 00, 000. Putting underscore at places of a comma
169
+ makes the larger value more readable than writing entire value together.
177
170
178
171
* Default arguments to Namedtuples:
179
172
180
- Namedtuples are less known, but very helpful feature of Python. I use them
181
- whenever I find constructing a Class can be lengthy, still I want to mimic the
182
- Class behaviour.
183
-
184
- Below is the example of putting a default arguments to a named tuple:
173
+ Namedtuples are less known, but very helpful feature of Python. I use them
174
+ whenever I find constructing a Class can be lengthy still, I want to mimic the
175
+ Class behavior.
185
176
186
- ``` python
187
- >> > from collections import namedtuple
188
- >> > Point = namedtuple(" Point" , [" x" , " y" ], defaults = [2 ,])
189
- >> > p = Point(3 )
190
- >> > p
191
- Point(x = 3 , y = 2 )
192
- ```
177
+ Below is the example of putting a default arguments to a named tuple:
193
178
194
- Explanation:
179
+ '''python
180
+ > ;> ;> ; from collections import namedtuple
181
+ > ;> ;> ; Point = namedtuple("Point", [ "x", "y"] , defaults=[ 2,] )
182
+ > ;> ;> ; p = Point(3)
183
+ > ;> ;> ; p
184
+ Point(x=3, y=2)
185
+ '''
195
186
196
- The default arguments will be assigned in to attributes in the order from left
197
- to right. Here, the default argument 2 will be assigned to y, and not
198
- x.
187
+ Explanation:
199
188
189
+ The default arguments will be assigned in the order from left to right. Here,
190
+ the default argument 2 will be assigned to y, and not x.
200
191
201
- * .pyc files are now diffed with hash approach:
192
+ * .pyc files are now diffed with the hash approach:
202
193
203
- .pyc are object files generated everytime you change your Python code file
204
- (.py). At present identifying the change in Python code is done by comparing
205
- meta fields like edited date. With this release, that functionality is
206
- improved by comparing files with hash based approach. Hashbased approach quick
207
- and more consistent than a meta data approach. Though this improvment is still
208
- considered unstable and CPython will continue with the meta data approach.
194
+ .pyc are object files generated every time you change your Python code file
195
+ (.py). At present identifying the change in Python code is done by comparing
196
+ meta fields like an edited date and few other fields. With this release, that
197
+ functionality is improved by comparing files with a hash-based approach. Hashed
198
+ approach is quick and more consistent than a metadata approach. Though this
199
+ improvement is still considered unstable and CPython will continue with the
200
+ metadata approach with this release.
0 commit comments