Clean Code in Python
Refactor your legacy code base
Mariano Anaya
BIRMINGHAM - MUMBAI
Clean Code in Python
Copyright 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Merint Mathew
Acquisition Editor: Denim Pinto
Content Development Editor: Priyanka Sawant
Technical Editor: Gaurav Gala
Copy Editor: Safis Editing
Project Coordinator: Vaidehi Sawant
Proofreader: Safis Editing
Indexer: Rekha Nair
Graphics: Jason Monteiro
Production Coordinator: Shantanu Zagade
First published: August 2018
Production reference: 1270818
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78883-583-1
www.packtpub.com
To my family and friends, for their unconditional love and support.
Mariano Anaya
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
Mariano Anaya is a software engineer who spends most of his time creating software with Python and mentoring fellow programmers. Mariano's main areas of interests besides Python are software architecture, functional programming, distributed systems, and speaking at conferences.
He was a speaker at Euro Python 2016 and 2017. To know more about him, you can refer to his GitHub account with the username rmariano.
His speakerdeck username is rmariano.
About the reviewer
Nimesh Kiran Verma has a dual degree in Maths and Computing from IIT Delhi and has worked with companies such as LinkedIn, Paytm and ICICI for about 5 years in software development and data science.
He co-founded a micro-lending company, Upwards Fintech and presently serves as its CTO. He loves coding and has mastery in Python and its popular frameworks, Django and Flask.
He extensively leverages Amazon Web Services, design patterns, SQL and NoSQL databases, to build reliable, scalable and low latency architectures.
To my mom, Nutan Kiran Verma, who made me what I am today and gave the confidence to pursue all my dreams. Thanks Papa, Naveen, and Prabhat, who motivated me to steal time for this book when in fact I was supposed to be spending it with them. Ulhas and the entire Packt team's support was tremendous. Thanks Varsha Shetty for introducing me to Packt.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
This is a book about software engineering principles applied to Python.
There are many books about software engineering, and many resources available with information about Python. The intersection of those two sets, though, is something that requires action, and that's the gap this book tries to bridge .
It would not be realistic to cover all possible topics about software engineering in a single book because the field is so wide that there are entire books dedicated to certain topics. This book focuses on the main practices or principles of software engineering that will help us write more maintainable code, and how to write it by taking advantage of the features of Python at the same time.
A word to the wise: there is no single solution to a software problem. It's usually about trade-offs. Each solution will have upsides and downsides, and some criteria must be followed to choose between them, accepting the costs and getting the benefits. There is usually no single best solution, but there are principles to be followed, and as long as we follow them we will be walking a much safer path. And that is what this book is about: inspiring the readers to follow principles and make the best choices, because even when facing difficulties, we will be much better off if we have followed good practices.
And, speaking of good practices, while some of the explanations follow established and proven principles, other parts are opinionated. But that doesn't mean it has to be done in that particular way only. The author does not claim to be any sort of authority on the matter of clean code, because such a title cannot possible exist. The reader is encouraged to engage in critical thinking: take what works the best for your project, and feel free to disagree. Differences of opinions are encouraged as long as they yield an enlightening debate.
My intention behind this book is to share the joys of Python, and idioms I have learned from experience, in the hope that readers will find them useful to elevate their expertise with the language.
The book explains the topics through code examples. These examples assume the latest version of Python at the time of this writing is used, namely Python 3.7, although future versions should be compatible as well. There are no peculiarities in the code that bind it to any particular platform, therefore with a Python interpreter, the code examples can be tested on any operating system.
Next page