While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
PYTHON: BEST PRACTICES TO PROGRAMMING CODE WITH PYTHON
First edition. January 5, 2017.
Copyright 2017 Charlie Masterson.
Written by Charlie Masterson.
10 9 8 7 6 5 4 3 2 1
Python:
Best Practices
to Programming Code
with Python
Charlie Masterson
Table of Contents
Copyright 2017 by Charlie Masterson - All rights reserved.
The contents of this book may not be reproduced, duplicated or transmitted without direct written permission from the author.
Under no circumstances will any legal responsibility or blame be held against the publisher for any reparation, damages, or monetary loss due to the information herein, either directly or indirectly.
Legal Notice:
This book is copyright protected. This is only for personal use. You cannot amend, distribute, sell, use, quote or paraphrase any part or the content within this book without the consent of the author.
Disclaimer Notice:
Please note the information contained within this document is for educational and entertainment purposes only. Every attempt has been made to provide accurate, up to date and reliable complete information. No warranties of any kind are expressed or implied. Readers acknowledge that the author is not engaging in the rendering of legal, financial, medical or professional advice. The content of this book has been derived from various sources. Please consult a licensed professional before attempting any techniques outlined in this book.
By reading this document, the reader agrees that under no circumstances are is the author responsible for any losses, direct or indirect, which are incurred as a result of the use of information contained within this document, including, but not limited to, errors, omissions, or inaccuracies.
I want to thank you and congratulate you for reading the book, Python: Best Practices to Programming Code with Python. This book contains proven steps and strategies on how to program in Python more effectively. When you first learn Python, you will be taught how to write code, but in many cases, you will not be taught how to write that code neatly.
If you speak to any Python programmer, ask them what it is they like about Python. I guarantee they will tell you that one of the main reasons is because it is easily readable. In fact, this is the absolute heart of the Python language, simply because computer code is read far more than it is written. Much of this is down to the fact that Python code follows a set of guidelines and Pythonic idioms if you hear a programmer refer to a part of the code as not being Pythonic, it means that it doesnt follow those guidelines and it doesnt express intent in any readable manner.
Code style and layout is incredibly important, as is consistency in the style that you use. With that said, there are times when consistency isnt the right thing and the guidelines are simply not applicable you have to know when that time is and that is when best judgment comes into programming with Python.
With this guide, I am going to show you the absolute best way to write your code, tidying up your program and making it all more effective and efficient. I wont just give you the basic. I will delve deep into everything you need to know, from the layout of the code to how to write functions, idioms, and names.
I will talk about whitespaces and tabs, strings, and methods. I will also be giving you some general recommendations for Python programming along with the general concepts. In short, by the time you have read my Best Practice guide, you will be the best Python programmer you can possibly be.
My book does assume some prior knowledge of Python programming, so if you are a complete newbie to the programming scene, please familiarize yourself with Python code before you read this book. My aim is to help you to produce Python code that is free from, or at least has very few, complications or obvious problems, as well as making it more readable for others.
Thanks again for reading this book, I hope you enjoy it!
Chapter 1:
General Concepts of Python Coding
B efore we get properly into the book, these are the general concepts of coding that you should be aware of; concepts that will make life easier for you.
Explicit Code
W HILE YOU CAN DO ALL sorts of weird and wonderful things with Python, it is best to keep it straightforward and explicit.
A Bad Example:
def make_complex(*args):
x, y = args
return dict(**locals())
A GOOD EXAMPLE:
def make_complex(x, y):
return {'x': x, 'y': y}
Compare these examples; in the latter example of code, we explicitly received x and y from the caller and the return is an explicit dictionary.
The developer who wrote this knows what to do just by looking at the first line and the last line; this is not the case with the bad example.
One Line, One Statement
W HILE THERE ARE COMPOUND statements, like the list comprehensions, that are allowed and, in many cases, appreciated, it is not good practice to have 2 statements that are disjointed on one code line.
A Bad Example:
print 'one'; print 'two'
if x == 1: print 'one'
if and :
# do something
A GOOD EXAMPLE:
print 'one'
print 'two'
I F X == 1:
print 'one'
cond1 =
cond2 =
if cond1 and cond2:
# do something
Returning Values
W HEN YOU GET A FUNCTION that is ever-growing in complexity, it isnt unheard of to use several return statements in the body of the function. However, if you want to maintain clear intent and a good level of readability, you should try not to return values from several points in the function body.
When it comes to returning values within a function, there are 2 cases for doing so: the result that comes from a normally processed function return, and the errors that are indicative of incorrect input parameters or for any reason that the function cannot complete the computation.