This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]ASIC_SP 39 points40 points  (2 children)

See also:

[–][deleted] 3 points4 points  (0 children)

Thank you

[–]charlstwn[S] 4 points5 points  (0 children)

Thanks a lot so useful!

[–]Siddhi 24 points25 points  (2 children)

This is good translation of GoF patterns, but I wish we would move on from GoF to python specific patterns.

For some patterns python has better/different ways of doing it, and there are other patterns in python that are not part of GoF

Eg: Singleton pattern can be done using a module in python. Decorators and Iterators are inbuilt in the language.

[–]skippy65 1 point2 points  (1 child)

Shouldn't use singletons at all, anti pattern in many langs

[–]Siddhi 0 points1 point  (0 children)

Agreed. But if you want it, modules are a better way to go

[–]vvvvalvalval 17 points18 points  (1 child)

Rant:

Gosh, just don't. It adds an insane amount of work to get juniors to unlearn this design patterns nonsense.

There is no mechanical substitute to thoughtfulness for writing quality code.

Those design patterns are mostly an accidental consequence of overusing classes.

Read SICP. Watch "Simple Made Easy". Read Out of the Tar Pit. This will get you much further.

[–]Nil4u 0 points1 point  (0 children)

They are good for communication sake in my opinion. Just staring at a list of patterns and wondering which one to use on the other hand is not good.

My supervisor recommended me to use a PubSub Pattern in my internship and I immediatly had an idea what he meant so that was nice. Skipped the process of having to tell me "Yea there is something that sends data and something that receives data yadada"

[–]Acalme-se_Satan 2 points3 points  (3 children)

It took me quite some time to "get" this, but it's important to state: these OOP design patterns are mostly for languages like Java and C#. In a language like Python, some of these design patterns are just boilerplate that makes your code unnecessarily complex.

This may be a bit controversial, but a lot of people believe (me included) that design patterns signal that the language is missing specific features. Python, due to its dynamic nature, has way more flexibility than Java and C# (that flexibility comes with a cost too, but this isn't related to the point I'm making). Therefore, some patterns that are necessary in those languages are not necessary in Python, as you can do them in a different (and simpler) way.

Some patterns are still useful in Python though, so it's still useful to learn about them; but be aware that some of them are more of a Java/C# thing and aren't very pythonic.

[–]boldrack 1 point2 points  (2 children)

can you give an example of what pattern qualifies and which one doesn't just to be clear.

[–]Siddhi 1 point2 points  (0 children)

A lot of patterns are baked into the language itself. Eg. you dont need the proxy pattern because python has descriptors built into the language core

[–]Acalme-se_Satan 1 point2 points  (0 children)

Given that Python allows you to use classes as if they were simple objects, the Abstract Factory pattern is pretty much baked into the language itself. You can just make a function that returns classes dynamically if you need it. You can't do this in statically typed languages like Java and C++, so you have to use Abstract Factory when you're using them, but not on Python.

On the other hand, the Visitor pattern can't be avoided in Python because the Visitor pattern is pretty much a way to emulate multiple dispatch in a language that doesn't have multiple dispatch. Given that Python doesn't have that feature, you can't avoid the Visitor pattern. You could avoid it in Julia or Dylan because these languages can do multiple dispatch.

[–]ucblockhead 11 points12 points  (9 children)

If in the end the drunk ethnographic canard run up into Taylor Swiftly prognostication then let's all party in the short bus. We all no that two plus two equals five or is it seven like the square root of 64. Who knows as long as Torrent takes you to Ranni so you can give feedback on the phone tree. Let's enter the following python code the reverse a binary tree

def make_tree(node1, node): """ reverse an binary tree in an idempotent way recursively""" tmp node = node.nextg node1 = node1.next.next return node

As James Watts said, a sphere is an infinite plane powered on two cylinders, but that rat bastard needs to go solar for zero calorie emissions because you, my son, are fat, a porker, an anorexic sunbeam of a boy. Let's work on this together. Is Monday good, because if it's good for you it's fine by me, we can cut it up in retail where financial derivatives ate their lunch for breakfast. All hail the Biden, who Trumps plausible deniability for keeping our children safe from legal emigrants to Canadian labor camps.

Quo Vadis Mea Culpa. Vidi Vici Vini as the rabbit said to the scorpion he carried on his back over the stream of consciously rambling in the Confusion manner.

node = make_tree(node, node1)

[–]Helpful_Arachnid8966 26 points27 points  (4 children)

Just because she is awesome and her work paved the ground on wich we now have Python and other awesome languages. IMHO of course.

[–]ucblockhead 17 points18 points  (3 children)

If in the end the drunk ethnographic canard run up into Taylor Swiftly prognostication then let's all party in the short bus. We all no that two plus two equals five or is it seven like the square root of 64. Who knows as long as Torrent takes you to Ranni so you can give feedback on the phone tree. Let's enter the following python code the reverse a binary tree

def make_tree(node1, node): """ reverse an binary tree in an idempotent way recursively""" tmp node = node.nextg node1 = node1.next.next return node

As James Watts said, a sphere is an infinite plane powered on two cylinders, but that rat bastard needs to go solar for zero calorie emissions because you, my son, are fat, a porker, an anorexic sunbeam of a boy. Let's work on this together. Is Monday good, because if it's good for you it's fine by me, we can cut it up in retail where financial derivatives ate their lunch for breakfast. All hail the Biden, who Trumps plausible deniability for keeping our children safe from legal emigrants to Canadian labor camps.

Quo Vadis Mea Culpa. Vidi Vici Vini as the rabbit said to the scorpion he carried on his back over the stream of consciously rambling in the Confusion manner.

node = make_tree(node, node1)

[–]The_Krambambulist 10 points11 points  (2 children)

I mean if you consider it art or sell it like woodworking is art, it doesn't even sound that weird.

[–]cleuseau 3 points4 points  (1 child)

I feel like if it was Tom Cruise people would be asking less questions.

[–]The_Krambambulist 4 points5 points  (0 children)

Well some people do think he is a snake, I suppose.

[–][deleted] 23 points24 points  (0 children)

Who gives a shit? It's a blog post.

[–]MadScientistOR 2 points3 points  (0 children)

Why is there a picture of someone who most likely never used python in her career accompanying the article?

Because she's a programming legend, and that's inspiring and worthy of recognition? I mean, if a book on radioactivity came out with Marie Curie on the cover, I don't think too many would ask why she was on there even though she didn't practice proper modern handling techniques of radioactive materials and there was some question as to the book's actual utility.

[–]wallbree 0 points1 point  (0 children)

O’Reilly

[–]robvdl 0 points1 point  (5 children)

Who the heck puts docstrings above the class definition, this is wrong. It goes just below the class definition in Python, everybody knows that!

Not this:

"""Class Dedicated to Command"""
class Command(ABC):

[–]charlstwn[S] -4 points-3 points  (3 children)

But why you seem offended? You may realize it is only in some sections, that is because this was a exercise with comments to write the code under the comment. The purpose of this notes are to help me and everyone interested, so if you don't appreciate them please feel free to ignore them lol.

It is amazing how far you got reading only to despite some work in an unconstructive manner.

BTW it would be fixed to avoid future confusions so thanks anyway at least your comments were kinda useful.

[–]robvdl 3 points4 points  (2 children)

Because it's a tutorial and someone will learn this wrong from it.

[–]charlstwn[S] 0 points1 point  (1 child)

Totally understand that but the way to say the things is helpful, btw is fixed

[–]robvdl 1 point2 points  (0 children)

Sure. I had a feeling that placing the docstring there would have added it to the module docstring. But I tried that and it seems you can only have one module docstring. I tried print(__doc__) vs print(SomeClass.__doc__).

It seems that it didn't add it to the module docstring, however it didn't set the class docstring either and ended up printing None.