Book Image

Expert Python Programming

By : Tarek Ziadé
Book Image

Expert Python Programming

By: Tarek Ziadé

Overview of this book

<p>Python is a dynamic programming language, used in a wide range of domains by programmers who find it simple, yet powerful. From the earliest version 15 years ago to the current one, it has constantly evolved with productivity and code readability in mind.<br /><br />Even if you find writing Python code easy, writing code that is efficient and easy to maintain and reuse is not so straightforward. This book will show you how to do just that:&nbsp; it will show you how Python development should be done. Python expert Tarek Ziadé takes you on a practical tour of Python application development, beginning with setting up the best development environment, and along the way looking at agile methodologies in Python, and applying proven object-oriented principles to your design.</p>
Table of Contents (21 chapters)
Credits
Foreword
About the Author
About the Reviewers
Preface
Index

Best Practices for Arguments


The signatures of functions and methods are the guardians of code integrity. They drive its usage and build its API. Besides the naming rules that we have seen previously, special care has to be taken for arguments. This can be done through three simple rules:

  • Build arguments by Iterative Design.

  • Trust the arguments and your tests.

  • Use *args and **kw magic arguments carefully.

Build Arguments by Iterative Design

Having a fixed and well-defined list of arguments for each function makes the code more robust. But this can't be done in the first version, so arguments have to be built by iterative design. They should reflect the precise use cases the element was created for, and evolve accordingly.

For instance, when some arguments are appended, they should have default values wherever possible to avoid any regression:

>>> class BD(object): # version 1
...     def _query(self, query, type):
...         print 'done'
...     def execute(self, query): 
...        ...