169

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

Ted Smith
  • 9,415
  • 16
  • 50
  • 52
  • Duplicate: http://stackoverflow.com/questions/127377/programming-languages-and-design-patterns, http://stackoverflow.com/questions/546479/do-oo-design-principles-apply-to-python, http://stackoverflow.com/questions/112358/what-are-some-good-web-resources-for-learning-object-oriented-programming – S.Lott Mar 04 '09 at 12:21
  • https://github.com/faif/python-patterns/blob/master/README.md – Kamesh Jungi Mar 02 '17 at 12:48

6 Answers6

152

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

mannykary
  • 812
  • 2
  • 10
  • 18
Ed.
  • 449
  • 2
  • 6
  • 9
  • 1
    The third link is dead. Tried to find mirror but couldn't, If anybody has the correct link please add – formatkaka Oct 27 '16 at 12:40
  • 1
    Same here, _Python Idioms and Efficiency_ doesn't seem to work. – Dawid Laszuk Jan 08 '17 at 10:14
  • The first and second links were dead so I updated them with Internet Archive snapshots. They seem to be very old though, although I'm sure a lot of it is still relevant. – mannykary Dec 28 '18 at 23:35
25

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.

Anonymous
  • 3,011
  • 4
  • 24
  • 23
13

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

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
leancz
  • 688
  • 5
  • 21
  • 2
    Note that according to the [wiki](http://wiki.python.org/moin/AdvancedBooks) this book is no longer maintained. – sakisk Jul 31 '11 at 20:34
  • updated link [thinking in python] (http://www.mindview.net/Books/Python/ThinkingInPython.html) – surge10 May 24 '18 at 13:00
6

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.

Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
6

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.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • 8
    I think that it is important to understand how to use idiomatic python for implementing design patterns. It is a very different language than C++ which is used in GOF thus porting from C++ to python will result in non-pythonic code. – sakisk Jul 31 '11 at 20:29
  • 1
    I tend to disagree. A singleton DP or other DP's will be possible in any OO language, whether it be Python, Java, or C++. What is non-pythonic in your opinion? – Paul Hiemstra Feb 23 '12 at 21:13
  • 1
    The first link is a fairly poor paper, with many mistaken assertions about Python. The second currently 404's, thus, I suppose, doing far less damage :) – Brandon Rhodes Jul 26 '12 at 21:56
3

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.

Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
  • 1
    The question was about Python; the paper talks about Scheme-like language that is, from what I can tell, invented on-the-spot for this particular paper. Unless the questioner is (a) an academic computer scientist with (b) long experience with Python practices, I doubt if they could get anything of value out of the paper — and, more to the point, they would probably not have asked this question in the first place. :) – Brandon Rhodes Jul 26 '12 at 21:59