473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

do design patterns still apply with Python?

Since Python does so many things different, especially compared to
compiled and statically typed languages, do most of the basic design
patterns still apply when writing Python code? If I were to read a
design pattern book (such as Head First Design Patterns), could I apply
their Java examples to Python easily enough, or does Python require a
different perspective when applying patterns?
Mar 2 '06 #1
24 2060
Hi John.

Design patterns aren't tied to any specific implementation so they
should apply to any language. You might find that they can even apply
to solution domains outside of computer science.

Implementation details will usually differ between languages. But the
implementation can also be different using the same language. For
example, you can use Java to implement the Model View Controller design
pattern when writing a web site or when writing a database engine --
the implementation will be very different, but both will be MVC.

In python, you might find it more natural to do design patterns in a
completely different way than they're implemented in Java. For
example, I've heard of using the Singleton pattern in python by just
implementing it as a module, no classes necessary.

sjbrown

Mar 2 '06 #2
sh******@gmail.com wrote:
In python, you might find it more natural to do design patterns in a
completely different way than they're implemented in Java. For
example, I've heard of using the Singleton pattern in python by just
implementing it as a module, no classes necessary.

Yeah, that's what I was wondering. I wonder if, after reading a DP book,
I might have to 'unlearn' some things when applying them to Python. But
I suppose I should just do it first and then try to implement them
myself. OOP is just so mind-bending for me that I've kind of put off
patterns right now until I get more comfortable with it. :)
Mar 2 '06 #3
On 2006-03-02, John Salerno <jo******@NOSPAMgmail.com> wrote:
Since Python does so many things different, especially compared to
compiled and statically typed languages, do most of the basic design
patterns still apply when writing Python code?


Definitely. Especially plaid, paisley, and a nice medium
houndstooth check. But please, not all at the same time.

--
Grant Edwards grante Yow! Maybe we could paint
at GOLDIE HAWN a rich PRUSSIAN
visi.com BLUE --
Mar 2 '06 #4
In article <8S******************@news.tufts.edu>,
John Salerno <jo******@NOSPAMgmail.com> wrote:
Since Python does so many things different, especially compared to
compiled and statically typed languages, do most of the basic design
patterns still apply when writing Python code? If I were to read a
design pattern book (such as Head First Design Patterns), could I apply
their Java examples to Python easily enough, or does Python require a
different perspective when applying patterns?


Many of the classic design patterns apply just fine to Python, at least in
the high-level view. On the other hand, much (most?) of what's in the
classic design pattern books is so tied up with ways around C++/Java type
bondage, it's difficult to see the forest for the trees.

For example, take the most classic of all patterns, Singleton. A typical
C++ Singleton treatment will be all about making constructors private and
shit like that. None of that carries over to Python. What I would do in
Python is have a module-level factory function which caches a single
instance of the class to return to the 2nd and subsequent caller and not
get my panties in a twist over the fact that some clever programmer could
find away around my code and force creation of a second instance.

The basic concepts in the pattern books are worth knowing. You just have
to be able to tease apart the concepts from the language-specific cruft
that so often obfuscates the descriptions.
Mar 2 '06 #5
John Salerno wrote:
Yeah, that's what I was wondering. I wonder if, after reading a DP book,
I might have to 'unlearn' some things when applying them to Python.
I would say adjust instead of unlearn. This is probably true to a
lesser or greater extent of any language for which your DP book was not
written.
But I suppose I should just do it first and then try to implement them
myself. OOP is just so mind-bending for me that I've kind of put off
patterns right now until I get more comfortable with it. :)


I would suggest getting a good grasp on OOP before you get into design
patterns. When most people start with any new concept they tend to try
and see everything in terms of their new toy, so sticking to one or two
new concepts at a time will make things a little easier.

Design patterns are kind of like sarcasm: hard to use well, not always
appropriate, and disgustingly bad when applied to problems they are not
meant to solve. You will do just fine without them until OOP is at
least familiar to you, and by that time you should be a little better
able to use them appropriately.

Mar 2 '06 #6
On Thu, 02 Mar 2006 16:38:44 -0500
Roy Smith <ro*@panix.com> wrote:
In article <8S******************@news.tufts.edu>,
John Salerno <jo******@NOSPAMgmail.com> wrote:
Since Python does so many things different, especially
compared to compiled and statically typed languages, do
most of the basic design patterns still apply when
writing Python code? If I were to read a design pattern
book (such as Head First Design Patterns), could I apply
their Java examples to Python easily enough, or does
Python require a different perspective when applying
patterns?

[...]
The basic concepts in the pattern books are worth knowing.
You just have
to be able to tease apart the concepts from the
language-specific cruft that so often obfuscates the
descriptions.


This sounds like an article crying out to be written,
"(Learning) Design Patterns with Python".

Has it been written already?

Cheers,
Terry

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 2 '06 #7
John Salerno <jo******@NOSPAMgmail.com> writes:
Since Python does so many things different, especially compared to
compiled and statically typed languages, do most of the basic design
patterns still apply when writing Python code? If I were to read a
design pattern book (such as Head First Design Patterns), could I
apply their Java examples to Python easily enough, or does Python
require a different perspective when applying patterns?


I'd say the relative importance of particular patterns depends on
whether the problems it solves are easier or harder in the language
you're using. Many things that are difficult in Java are easy in
Python, so the patterns in the book aren't terribly relevant.
Somewhat less often, something is easy in Java and difficult in
Python. When that happens, you may need approaches that aren't in the
book. Finally, you have to think of all of these books as different
approaches of explaining elephants to blind men. There's a lot of
murkiness in programming and design patterns are yet another way to
help guide people through it. But the more programming you do, the
better your own internal maps and instincts become, and the less you
need a guidebook that doesn't always correspond to the actual
landscape you're operating in. So consider the books to contain
helpful guidelines but don't feel afraid to trust your own instincts
over them, especially after those instincts become better developed.
Mar 3 '06 #8
In article <7x************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
Somewhat less often, something is easy in Java and difficult in
Python.


Example?
Mar 3 '06 #9
Roy Smith <ro*@panix.com> writes:
Somewhat less often, something is easy in Java and difficult in
Python.

Example?


Sandboxed code is a real obvious one.
Mar 3 '06 #10
In article <7x************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
Roy Smith <ro*@panix.com> writes:
Somewhat less often, something is easy in Java and difficult in
Python.

Example?


Sandboxed code is a real obvious one.


What is "sandboxed code"?
Mar 3 '06 #11
Roy Smith <ro*@panix.com> writes:
Sandboxed code is a real obvious one.

What is "sandboxed code"?


It's what the old rexec/bastion module tried unsuccessfully to
implement: a way of running potentially hostile code while limiting
the kinds of harmful stuff it can do. This is needed for things like
browser applets. Javascript (not related to Java except by name) also
depends on it. A web browser that let arbitrary web pages run python
scripts would be totally insecure and would let the site implementers
take over your browser. Obviously not every application needs this,
but it's hard to do unless there's fairly deep support for it in the
language.
Mar 3 '06 #12
"ajones" <aj*****@gmail.com> writes:
Design patterns are kind of like sarcasm: hard to use well, not always
appropriate, and disgustingly bad when applied to problems they are not
meant to solve.


+1 QOTW
Mar 3 '06 #13

Terry Hancock wrote:

This sounds like an article crying out to be written,
"(Learning) Design Patterns with Python".

Has it been written already?

Cheers,
Terry

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com


There's a couple Alex M slideshows and a couple discussions of
Creational/Structural / Behavioral patterns

http://www.strakt.com/docs/ep04_pydp.pdf

http://www.suttoncourtenay.org.uk/du...npatterns.html
http://www.pasteur.fr/formation/info...n/ch18s06.html
http://www.sauria.com/~twl/conferenc...%20Design.html

i think mostly i flip thru the Oreilly cookbook whenever i need
something.

Mar 3 '06 #14
ajones wrote:

(snip)
I would suggest getting a good grasp on OOP before you get into design
patterns. When most people start with any new concept they tend to try
and see everything in terms of their new toy, so sticking to one or two
new concepts at a time will make things a little easier.

Design patterns are kind of like sarcasm: hard to use well, not always
appropriate, and disgustingly bad when applied to problems they are not
meant to solve. You will do just fine without them until OOP is at
least familiar to you, and by that time you should be a little better
able to use them appropriately.


<aol>Well, I mostly agree here</aol> but OTOH, studying the GoF has been
a real 'OO mind-opener' for me.

I'd say the key here is to study existing patterns to try and understand
how to best design and structure OO code instead of insisting on
copy/pasting canonical patterns everywhere (IOW : trying to get the
'spirit' of patterns instead of sticking to the 'letter').

My 2 cents...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Mar 3 '06 #15
Paul Rubin wrote:
Roy Smith <ro*@panix.com> writes:
Somewhat less often, something is easy in Java and difficult in
Python.

Example?


Sandboxed code is a real obvious one.


I don't disagree that this is true in general, but is that actually
covered in the design patterns book [1] or in other related literature?

Paul

[1] "Design Patterns: Elements of Reusable Object-Oriented Software"

Mar 3 '06 #16
ajones <aj*****@gmail.com> wrote:
I would suggest getting a good grasp on OOP before you get into design
patterns. When most people start with any new concept they tend to try
and see everything in terms of their new toy, so sticking to one or two
new concepts at a time will make things a little easier.
Having read the design patterns book a long time after learning OOP, I
came at it with a different perspective. I found it was useful for
naming techniques which I'd been using all along. A good programmer
will find it easy to re-invent nearly all the patterns, but having a
name for them is important.

As programmers we name everything and as a corollary if it hasn't got
a name it is difficult to talk about, so from that angle the book is
good and very applicable to python. That said I found wading though
pages of language-specific waffle extremely dull!
Design patterns are kind of like sarcasm: hard to use well, not always
appropriate, and disgustingly bad when applied to problems they are not
meant to solve. You will do just fine without them until OOP is at
least familiar to you, and by that time you should be a little better
able to use them appropriately.


;-)

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Mar 3 '06 #17
Personally, I find many of the design patterns apply but require
modification.

For example, the Factory pattern is mostly to work around the fact that
it's difficult in Java and C++ to dynamically load classes. Not so in
Python, especially with exec. A simple configuration file and an exec
call can replace an entire Factory pattern, with the same result.

The ideas are fine, but sometimes it's best to keep things simple. I
find that DP junkies don't tend to keep things simple.

Mar 3 '06 #18
In article <11**********************@z34g2000cwc.googlegroups .com>,
"msoulier" <ms******@gmail.com> wrote:
For example, the Factory pattern is mostly to work around the fact that
it's difficult in Java and C++ to dynamically load classes.


You're over-specifying. Most of most design patterns is to work around the
fact that it's difficult in Java and C++ to do many things.
Mar 3 '06 #19
Roy Smith wrote:
For example, the Factory pattern is mostly to work around the fact that
it's difficult in Java and C++ to dynamically load classes.


You're over-specifying. Most of most design patterns is to work around the
fact that it's difficult in Java and C++ to do many things.


+1 QOTW!

--Irmen
Mar 3 '06 #20
gene tani <ge*******@gmail.com> wrote:
...
There's a couple Alex M slideshows and a couple discussions of
Creational/Structural / Behavioral patterns

http://www.strakt.com/docs/ep04_pydp.pdf
And more of my stuff (including slideshows on Design Patterns in Python,
and one essay "Five easy pieces" on the Borg DP) can be found by
starting at my homepage, http://www.aleax.it -- I've long dreamed of
writing a book about Design Patterns and methodology in/for/with Python,
but, it's unlikely to happen any time soon.
i think mostly i flip thru the Oreilly cookbook whenever i need
something.


Yes, we have substantial numbers of DPs there, too, both 1st and 2nd
editions (more, I believe in the 2nd ed).
Alex
Mar 4 '06 #21
"Paul Boddie" <pa**@boddie.org.uk> writes:
Sandboxed code is a real obvious one.


I don't disagree that this is true in general, but is that actually
covered in the design patterns book [1] or in other related literature?


It's been a while since I looked at the design patterns book and I
don't know whether it discusses sandboxing. I'd guess probably not.
As for whether any of the GoF book patterns are easy in Java but
difficult in Python, it's a good question and I don't know the answer.
Maybe the book has some patterns involving interfaces. I'm a rather
low-tech Java user and I'm not a fan of the language, so I'm not a
good person to ask.
Mar 4 '06 #22
Paul Novak 写道:
A lot of the complexity of design patterns in Java falls away in
Python, mainly because of the flexibility you get with dynamic typing.
I agree with this very much !
In java or C++ or all such static typing and compiled languages , the
type is fixed on
in the compile phrase , so for the flexible at the runtime , we often
need to program to
interface . For example ,
we do in java :

implement I{...}
class A implement I{...}
class B implement I{...}

oprate(I var) // we can pass A's instance or B's instance here

and in C++ :

class Abstract{...}
class A : Abstract{...}
class B : Abstract{...}

oprate(Abstract var) // pass the A's instance or B's instance here

But in python , type is dynamic , and name is bind at runtime , so we
can pass any variable as we want ! This feather make python not need for
redundant class inherits
and interfaces which are the core of the GoF's design patterns I think !
For a Pythonic Perspective on Patterns, "Python Programming Patterns"
by Thomas W. Christopher is definitely worth tracking down. It looks
like it is out of print, but you can find used copies on Amazon.

Regards,

Paul.
This sounds like an article crying out to be written,
"(Learning) Design Patterns with Python".

Has it been written already?

Cheers,
Terry
Bruce Eckel began writing "Thinking In Python" it was last updated
in 2001.


Mar 4 '06 #23
Paul Novak :
A lot of the complexity of design patterns in Java falls away in
Python, mainly because of the flexibility you get with dynamic typing.
I agree with this very much !
In java or C++ or all such static typing and compiled languages , the
type is fixed on
in the compile phrase , so for the flexible at the runtime , we often
need to program to
interface . For example ,
we do in java :

implement I{...}
class A implement I{...}
class B implement I{...}

oprate(I var) // we can pass A's instance or B's instance here

and in C++ :

class Abstract{...}
class A : Abstract{...}
class B : Abstract{...}

oprate(Abstract var) // pass the A's instance or B's instance here

But in python , type is dynamic , and name is bind at runtime , so we
can pass any variable as we want ! This feather make python not need for
redundant class inherits
and interfaces which are the core of the GoF's design patterns I think ! For a Pythonic Perspective on Patterns, "Python Programming Patterns"
by Thomas W. Christopher is definitely worth tracking down. It looks
like it is out of print, but you can find used copies on Amazon.

Regards,

Paul.

This sounds like an article crying out to be written,
"(Learning) Design Patterns with Python".

Has it been written already?

Cheers,
Terry
Bruce Eckel began writing "Thinking In Python" it was last updated
in 2001.


Mar 4 '06 #24
msoulier wrote:
I find that DP junkies don't tend to keep things simple.


+1 QOTW. There's something about these "political" threads that seems
to bring out the best quotes. b^)

Mar 4 '06 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tony Ha | last post by:
Hello I am learning Python for in the pass ten months, and have brought a few books about Python. Most of them are good books by its only right, and all of them only teach you how to write...
4
by: Ian Giblin | last post by:
I am an experienced C programmer, learning C++ by writinging a mathematical toolkit in the framework of a script interpreter. I am posting here to ask for advice (or references) on the object...
1
by: Jay | last post by:
The GOF text is widely considered the definitive book on the topic. Design Patterns: Elements of Reusable Object-Oriented Softare, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides ...
1
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The...
12
by: Jean-pierre Martineau | last post by:
how apply design patterns to c ?
6
by: Daniel Santa Cruz | last post by:
Hello all, I've been trying to go over my OO Patterns book, and I decided to try to implement them in Python this time around. I figured this would help me learn the language better. Well,...
6
by: Gabriel Genellina | last post by:
Hello Most authors talk about Java/C++, and describe patterns used as a workaround to their static class model; the dynamic nature of Python allows for trivial implementations in some cases....
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.