python design patterns

PythonDesign Patterns

Python Problem Overview


I am looking for any resources that gives examples of Best Practices, Design patterns and the SOLID principles using Python.

Python Solutions


Solution 1 - Python

Some overlap in these

Intermediate and Advanced Software Carpentry in Python

Code Like a Pythonista: Idiomatic Python

Python Idioms and Efficiency

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

Solution 2 - Python

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.

Solution 3 - Python

Bruce Eckel's Thinking in Python leans heavily on Design Patterns

Solution 4 - 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.

Solution 5 - Python

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][1] (to which I was introduced in [Python Cookbook][2]).

> 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. [1]: http://code.activestate.com/recipes/68205-null-object-design-pattern/ [2]: http://books.google.com.ar/books?id=Q0s6Vgb98CQC&pg=PT311&lpg=PT311&dq=Null+object+python+cookbook&source=bl&ots=hc3368UkpC&sig=qRrDqvZwg4FIoMuKo3JJhI7Bu_g&hl=es&ei=TiqpS5jZEMuVtgf08YGnAQ&sa=X&oi=book_result&ct=result&resnum=4&ved=0CBYQ6AEwAw#v=onepage&q=&f=false

Solution 6 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTed SmithView Question on Stackoverflow
Solution 1 - PythonEd.View Answer on Stackoverflow
Solution 2 - PythonAnonymousView Answer on Stackoverflow
Solution 3 - PythonleanczView Answer on Stackoverflow
Solution 4 - PythonBrian R. BondyView Answer on Stackoverflow
Solution 5 - PythonEsteban KüberView Answer on Stackoverflow
Solution 6 - PythonGilad NaorView Answer on Stackoverflow