Object Oriented vs Pythonic Code, and Pythonic standards

Roy Smith roy at panix.com
Tue Feb 7 14:51:18 EST 2006


Carl J. Van Arsdall <cvanarsdall at mvista.com> wrote:
> My question is, should we as python developers be trying to write
> code that follows more of a python standard or should we try to
> spend our efforts to stick to a more traditional OO model?

There is much about traditional OO which translates well to Python,
but sometimes it is difficult to read a treatise on OO and tell which
bits are "traditional OO" and which are "OO in C++/Java".

Traditional OO teaches that tight coupling of classes is bad, and this
is just as true in Python as it is in any language.

Many OO adherents also push lots of unit testing and test-driven
development; this works just as well (perhaps even better) in Python
as it does in other langauges.

> For example, in C++ I might make a file that defines a class and all
> its methods, at which point I create an object and do things with
> it.  My interpretation of what is "Pythonic" would be instead of
> creating a class I would just define functions and maybe some
> variables global to a module.  At this point, I import the module
> and just make function calls.

In general, defining classes and methods works in Python just like it
does in C++.  Your idea of making a module just a collection of functions
would correspond to defining a bunch of functions inside a namespace
in C++.  There are times when it might make sense, such as the math
module, but it would be unusual.

>There are many similarities here, but the difference is that in python I 
>don't feel as though I would define a class, I would just treat the 
>python module as a class instead, especially if it was a type of object 
>that I would only need a single instance of.

Why not?  Read up on the "singleton pattern".

Where OO in Python differs significantly from OO in C++ is anything
having to do with strict type checking.  There are lots of design
patterns that have to do with insulating classes from the types of
things they interact with.  In Python, that's mostly a non-issue
because of Python's dynamic typing.  If you ever see yourself
obsessing over the types of arguments, that's the time to ask yourself
if you're trying to do something unpythonic.

Another place where OO in Python tends to be at odds with OO in
C++/Java is data hiding.  Traditional OO dogma is that ALL data should
be private, accessed externally only through dedicated get()/set()
methods.  Python code tends to reject that (for reasons that I only
partially agree with).  In C++, you would say foo.setValue(x), but in
Python you would just say foo.Value = x.  There's nothing that says
you can't make all data private (with the underscore syntax) and write
setter/getter methods, but that's not the style that's commonly used.
Part of the reason is because in Python you can intercept all
attribute access with __getattribute__(), and __setattribute__(), but
I suspect another part of it is an emotional reaction to C++'s B&D
style of coding.  I'm not trying to persuade you one way or the other
here, but you should be aware of both styles.



More information about the Python-list mailing list