Sunil Kapil
Clean Python Elegant Coding in Python
Sunil Kapil
Sunnyvale, CA, USA
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484248775 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-4877-5 e-ISBN 978-1-4842-4878-2
https://doi.org/10.1007/978-1-4842-4878-2
Sunil Kapil 2019
Apress Standard
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
Python is one of the most popular languages today. Relatively new fields such as data science, AI, robotics, and data analytics, along with traditional professions such as web development and scientific research, are embracing Python. Its increasingly important for programmers writing code in a dynamic language like Python to make sure that the code is high-quality and error-free. As a Python developer, you want to make sure that the software you are building makes your users happy without going over budget or never releasing.
Python is a simple language, yet its difficult to write great code because there arent many resources that teach how to write better Python code.
Currently lacking in the Python world are code consistency, patterns, and an understanding of good Pythonic code among developers. For every Python programmer, great Pythonic code has a different meaning. The reason for this could be that Python is being used in so many areas that its difficult to reach consensus among developers about specific patterns. In addition, Python doesnt have any books about clean code like Java and Ruby do. There have been attempts to write those kinds of books to bring clarity to good Python practices, but those attempts have been few and far between, and quickly frankly, they havent been high-quality.
The main goal of this book is to provide tips to Python developers of various levels so they can write better Python software and programs. This book gives you various techniques irrespective of the field you use Python in. This book covers all levels of Python, from basic to advanced, and shows you how to make your code more Pythonic.
Remember, writing software is not only science but art, and this book will teach you how to become a better Python programmer.
Acknowledgments
First, I would like to thank Nikhil of Apress. Nikhil contacted me in October 2018 and persuaded me to write a book with Apress Media LLC. Next, I would like to thank Divya Modi, coordinating editor at Apress, for her great support while writing the chapters and her great patience during my busy schedule. In addition, many thanks to Rita Fernando, development editor at Apress, who provided valuable suggestions during the review process that made the book more valuable for Python developers. Next, I would like to thank Sonal Raj for critically examining every single chapter. You found many issues that I would never have found.
Of course, I would like to say a big thank-you to the whole production team at Apress for supporting me.
Last but not least, I would like to thank my beloved and unique family, especially for their understanding that a book project takes a great deal of time. Thanks to my mother, Leela Kapil, and father, Harish Chandra Kapil, for all the encouragement and support.
My beloved wife Neetu: I deeply appreciate all your encouragement and support while writing this book; it has made all the difference. You are awesome!
Table of Contents
About the Author and About the Technical Reviewer
About the Author
Sunil Kapil
has been in the software profession for the past ten years, writing production code in Python and several other languages. He has worked as a software engineer primarily on the backend for web and mobile services. He has developed, deployed, and maintained small to large projects in production that are being loved and used by millions of users. He has completed these projects with small to large teams in different professional environments for well-known software companies around the world. He is also a passionate advocate of open source and continuously contributes to projects such as Zulip Chat and Black. Additionally, he works with nonprofit organizations and contributes to their software projects on a volunteer basis.
Sunil is a frequent speaker at various meetups and conferences and has given frequent talks about Python.
You can visit his web site about software engineering, tools, and techniques. On top of that, you can reach out to him by e-mail or follow him on social media.
Web: https://softwareautotools.com/
E-mail: snlkapil@gmail.com
Twitter: @snlkapil ( https://twitter.com/snlkapil )
LinkedIn: https://www.linkedin.com/in/snlkapil/
GitHub: https://github.com/skapil
About the Technical Reviewer
Sonal Raj
( @_sonalraj ) has been an author, engineer, mentor, and avid Pythonista for more than 10 years. He is a Goldman Sachs alumnus and a former research fellow at the Indian Institute of Science. He is an integral part of the financial technology industry with expertise in building trading algorithms and low latency systems. He is an open source developer and community member.
Sonal has masters degrees in information technology and business administration. His domains of research include distributed systems, graph databases, and education technology. He is an active member of the Institution of Engineering and Technology (IET), London, and a lifetime member of the Indian Society for Technical Education.