<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator> <link href="https://blog.jaysinh.dev/tags/Python3.7/feed.xml" rel="self" type="application/atom+xml"/> <link href="https://blog.jaysinh.dev/" rel="alternate" type="text/html"/> <updated>2026-08-01T12:49:14+00:00</updated> <id>https://blog.jaysinh.dev/</id> <title type="html">Jaysinh’s own heed</title> <subtitle>I am a Full-stack developer by profession, Computer scientist by heart and an Actor by gene. I write mostly on programming topics. Browse through my blog posts to identify my taste of writing. </subtitle> <entry> <title type="html">Python 3.7 feature walkthrough</title> <link href="https://blog.jaysinh.dev/2019/01/12/python-3-7-feature-walkthrough.html" rel="alternate" type="text/html" title="Python 3.7 feature walkthrough"/> <published>2019-01-12T19:53:12+00:00</published> <updated>2019-01-12T19:53:12+00:00</updated> <id>https://blog.jaysinh.dev/2019/01/12/python-3-7-feature-walkthrough</id> <content type="html" xml:base="https://blog.jaysinh.dev/2019/01/12/python-3-7-feature-walkthrough.html">&lt;p&gt;In this post, I will explain improvements done in Core Python version 3.7. Below is the outline of features covered in this post.&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;p&gt;Breakpoints&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Subprocess&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Dataclass&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Namedtuples&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Hash-based Python object file&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt; &lt;h3 id=&quot;breakpoint&quot;&gt;breakpoint()&lt;/h3&gt; &lt;p&gt;Breakpoint is an extremely important tool for debugging. Since I started learning Python, I am using the same API for putting breakpoints. With this release, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breakpoint()&lt;/code&gt; is introduced as a built-in function. Because it is in a built-in scope, you don’t have to import it from any module. You can call this function to put breakpoints in your code. This approach is handier than importing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pdb.set_trace()&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;&lt;img src=&quot;/assets/images/walkthrough_python_3_7/breakpoint_example.gif&quot; alt=&quot;Breakpoint function in Python 3.7&quot; /&gt;&lt;/p&gt; &lt;p&gt;Code used in above example&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;breakpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;h3 id=&quot;pythonbreakpoint&quot;&gt;PYTHONBREAKPOINT&lt;/h3&gt; &lt;p&gt;There wasn’t any handy option to disable or enable existing breakpoints with a single flag. But with this release, you can certainly reduce your pain by using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PYTHONBREAKPOINT&lt;/code&gt; environment variable. You can disable all breakpoints in your code by setting the environment variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PYTHONBREAKPOINT&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;&lt;img src=&quot;/assets/images/walkthrough_python_3_7/breakpoint_environment_variable_example.gif&quot; alt=&quot;Breakpoint environment variable in Python 3.7&quot; /&gt;&lt;/p&gt; &lt;h5 id=&quot;i-advise-putting-pythonbreakpoint0-in-your-production-environment-to-avoid-unwanted-pausing-at-forgotten-breakpoints&quot;&gt;I advise putting “PYTHONBREAKPOINT=0” in your production environment to avoid unwanted pausing at forgotten breakpoints&lt;/h5&gt; &lt;h3 id=&quot;subprocessruncapture_outputtrue&quot;&gt;Subprocess.run(capture_output=True)&lt;/h3&gt; &lt;p&gt;You can pipe the output of Standard Output Stream (stdout) and Standard Error Stream (stderr) by enabling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capture_output&lt;/code&gt; parameter of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subprocess.run()&lt;/code&gt; function.&lt;/p&gt; &lt;p&gt;&lt;img src=&quot;/assets/images/walkthrough_python_3_7/subprocess_run_capture_output.gif&quot; alt=&quot;subprocess.run got capture_output parameter&quot; /&gt;&lt;/p&gt; &lt;p&gt;You should note that it is an improvement over piping the stream manually. For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subprocess.run([&quot;ls&quot;, &quot;-l&quot;, &quot;/var&quot;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)&lt;/code&gt; was the previous approach to capture the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt;.&lt;/p&gt; &lt;h3 id=&quot;dataclasses&quot;&gt;Dataclasses&lt;/h3&gt; &lt;p&gt;The new class level decorator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@dataclass&lt;/code&gt; introduced with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataclasses&lt;/code&gt; module. Python is well-known for achieving more by writing less. It seems that this module will receive more updates in future which can be applied to reduce significant line of code. Basic understanding of Typehints is expected to understand this feature.&lt;/p&gt; &lt;p&gt;When you wrap your class with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@dataclass&lt;/code&gt; decorator, the decorator will put obvious constructor code for you. Additionally, it defines a behaviour for dander methods &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__repr__()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__eq__()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__hash__()&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;&lt;img src=&quot;/assets/images/walkthrough_python_3_7/dataclasses_dataclass.gif&quot; alt=&quot;Dataclasses.dataclass&quot; /&gt;&lt;/p&gt; &lt;p&gt;Below is the code before introducing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataclasses.dataclass&lt;/code&gt; decorator.&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;After wrapping with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@dataclass&lt;/code&gt; decorator it reduces to below code&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;h3 id=&quot;namedtuples&quot;&gt;Namedtuples&lt;/h3&gt; &lt;p&gt;The namedtuples are a very helpful data structure, yet I found it is less known amongst developers. With this release, you can set default values to argument variables.&lt;/p&gt; &lt;p&gt;&lt;img src=&quot;/assets/images/walkthrough_python_3_7/namedtuple_example.gif&quot; alt=&quot;Namedtuples with default arguments&quot; /&gt;&lt;/p&gt; &lt;h5 id=&quot;note-default-arguments-will-be-assigned-from-left-to-right-in-the-above-example-default-value-2-will-be-assigned-to-variable-y&quot;&gt;Note: Default arguments will be assigned from left to right. In the above example, default value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; will be assigned to variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;&lt;/h5&gt; &lt;p&gt;Below is the code used in the example&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namedtuple&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Point&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;namedtuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,])&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;h3 id=&quot;pyc&quot;&gt;.pyc&lt;/h3&gt; &lt;p&gt;&lt;strong&gt;.pyc&lt;/strong&gt; are object files generated everytime you change your code file (.py). It is a collection of meta-data created by an interpreter for an executed code. The interpreter will use this data when you re-execute this code next time. Present approach to identify an outdated object file is done by comparing meta fields of source code file like last edited date. With this release, that identification process is improved by comparing files using a hash-based approach. The hash-based approach is quick and consistent across various platforms than comparing last edited dates. This improvement is considered unstable. Core python will continue with the metadata approach and slowly migrate to the hash-based approach.&lt;/p&gt; &lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt; &lt;ul&gt; &lt;li&gt; &lt;p&gt;Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breakpoint()&lt;/code&gt; will put a breakpoint in your code.&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Disable all breakpoints in your code by setting an environment variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PYTHONBREAKPOINT=0&lt;/code&gt;.&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subprocess.run([...], capture_output=True)&lt;/code&gt; will capture the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt;.&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Class level decorator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@dataclass&lt;/code&gt; will define default logic for constructor function. It will implement default logic for dunder methods &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__repr__()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;___eq__()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__hash__()&lt;/code&gt;.&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Namedtuple data structure supports default values to its arguments using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defaults&lt;/code&gt;.&lt;/p&gt; &lt;/li&gt; &lt;li&gt; &lt;p&gt;Outdated Python object files (.pyc) are compared using the hash-based approach.&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt; &lt;p&gt;I hope you were able to learn something new by reading this post. If you want to read an in-depth discussion on each feature introduced in Python 3.7, then please read &lt;a href=&quot;https://docs.python.org/3.7/whatsnew/changelog.html#python-3-7-0-final&quot;&gt;this&lt;/a&gt; official post. Happy hacking!&lt;/p&gt; &lt;h6 id=&quot;proofreaders-jason-braganza-ninpo-basen_-from-python-at-freenode-ultron-from-python-offtopic-at-freenode-upime-from-english-at-freenode&quot;&gt;Proofreaders: &lt;a href=&quot;https://janusworx.com/&quot;&gt;Jason Braganza&lt;/a&gt;, Ninpo, basen_ from #python at Freenode, Ultron from #python-offtopic at Freenode, up|ime from ##English at Freenode&lt;/h6&gt;</content> <author> <name>Jaysinh Shukla</name> </author> <category term="Python"/> <category term="CorePython"/> <category term="Python3.7"/> <summary type="html">In this post, I will explain improvements done in Core Python version 3.7. Below is the outline of features covered in this post.</summary> </entry> </feed>