I am looking for any resources that gives examples of Best Practices, Design patterns and the SOLID principles using Python.
You can get started here and here.
For a more in depth look at design pattners you should look at Design Patterns: Elements of Reusable Object-Oriented Software. The source code is not in Python, but it doesn't need to be for you to understand the patterns.
Some overlap in these
Intermediate and Advanced Software Carpentry in Python
Code Like a Pythonista: Idiomatic Python
Google Developers Day US - Python Design Patterns
Another resource is by example at the Python Recipes. A good number do not follow best practices but you can find some patterns in there that are useful
You may also wish to read this article (select the .pdf file), which discusses Design Patterns in dynamic object oriented languages (i.e. Python). To quote the page:
This paper explores how the patterns from the "Gang of Four", or "GOF" book, as it is often called, appear when similar problems are addressed using a dynamic, higher-order, object-oriented programming language. Some of the patterns disappear -- that is, they are supported directly by language features, some patterns are simpler or have a different focus, and some are essentially unchanged.
Bruce Eckel's "Thinking in Python" leans heavily on Design Patterns
Type
>>> import this
in a Python console.
Although this is usually treated as a (fine!) joke, it contains a couple of valid python-specific axioms.
Exper Python Programming http://books.google.com/books?id=vgPpOAAACAAJ&dq=exper+python+programming&hl=pl
Something you can use to simplify your code when calling attributes on objects that might or might not exist is to use the Null Object Design Pattern (to which I was introduced in Python Cookbook).
Roughly, the goal with Null objects is to provide an 'intelligent' replacement for the often used primitive data type None in Python or Null (or Null pointers) in other languages. These are used for many purposes including the important case where one member of some group of otherwise similar elements is special for whatever reason. Most often this results in conditional statements to distinguish between ordinary elements and the primitive Null value.
This object just eats the lack of attribute error, and you can avoid checking for their existence.
It's nothing more than
class Null(object):
def __init__(self, *args, **kwargs):
"Ignore parameters."
return None
def __call__(self, *args, **kwargs):
"Ignore method calls."
return self
def __getattr__(self, mname):
"Ignore attribute requests."
return self
def __setattr__(self, name, value):
"Ignore attribute setting."
return self
def __delattr__(self, name):
"Ignore deleting attributes."
return self
def __repr__(self):
"Return a string representation."
return "<Null>"
def __str__(self):
"Convert to a string and return it."
return "Null"
With this, if you do Null("any", "params", "you", "want").attribute_that_doesnt_exists()
it won't explode, but just silently become the equivalent of pass
.
Normally you'd do something like
if obj.attr:
obj.attr()
With this, you just do:
obj.attr()
and forget about it. Beware that extensive use of the Null
object can potentially hide bugs in your code.