How does Python's OOP feel?
ken at seehart.com
Sat May 30 06:01:20 EDT 2009
Benjamin Kaplan wrote:
> On Fri, May 29, 2009 at 9:41 PM, Lie Ryan <lie.1296 at gmail.com
> <mailto:lie.1296 at gmail.com>> wrote:
> Ikon wrote:
> > I'm rather new to Python. I have PHP for my main language and I do
> > some Java. They all have a very strict OO schema. As I red through
> > Python's tutorial it seams it has nothing of those rules. No
> > abstract classes, functions, or variables.
> > I wish someone, who has experience in both Java/PHP/C# and Python
> > would tell me how mush better/worse is it to program in a language
> > that misses most of the OO parts!
Some of the concepts that you are looking for might be present in Python
but may look different than you expect. If you are defining OO in
terms of rules and constraints, Python might come up short. But if you
are looking for the /benefits/ of OOP, Python does quite well.
I have experience with all of those languages. More Java than PHP and
C#, so I can talk about Python and Java.
Originally, the definition of OOP was about concepts such as loose
coupling, reusable components, focusing on things rather than
procedures, etc. Later the prevalent definition became more about what
you are not allowed to do than how to do what you want to do. So now we
put more stress on ideas like strict encapsulation. For example, some
purists even suggest that you can't claim to implement encapsulation
unless you guarantee that there is no possible way to determine
implementation details from outside. Python "fails" here because it has
introspective features that allow you to bypass the limitations that are
deemed necessary for some artificially strict definition of OOP. For
example strictly speaking we don't have private members, but we can
start a name with underscore or double-underscore which for all
practical purposes gives all the advantages of private members if we
assume a certain level of maturity from our programmers. A less
restrictive notion of encapsulation might be that objects can be managed
without the need to know the implementation details.
If you think in terms of the enabling qualities of OOP, but not the
constraints, Python does in fact score quite high. We have multiple
inheritance, abstraction (in the non-fascist sense), encapsulation
(again in the non-fascist sense), really easy polymorphism, and plenty
of support for decoupling.
When people claim that python is not OO, they are for the most part only
able to cite what python fails to prevent you from doing, not what
python fails to allow you to do. At the same time, I must admit that
the constraints imposed by stricter languages, such as compile time
detection of certain errors, and performance benefits associated with
static typing. Fortunately, tools such as pylint and psyco can close
these two respective gaps to a significant degree.
Okay, enough theory and straw man arguments :) Here's how it /feels/ to me.
What is missing from Python, as compared to Java, is an oppressive
system to prevent you from writing bugs no matter what. As it turn out,
I found that I was quite capable of writing bugs in Java in spite of
it's extreme efforts to save me from myself. I also found that Java
requires a great deal more lines of code to get to the desired results.
Most of the lines of code are about making the compiler happy. If I try
to do anything metaprogrammingish in Java, I soon start pulling my hair
out. After switching to mostly python, I have grown nearly all of my
Once the Java compiler is happy with the syntax, certain kinds of bugs
are prevented that might exist in corresponding python code. This is a
win for Java. However, because the code is more complex in Java, and
because it is harder to add clever introspective debugging
instrumentation, the remaining bugs are harder to find. I believe that
all other things being equal, less code is more robust than more code.
I highly recommend using pylint, BTW. If you use pylint, you are just
about as safe as you would be with a more oppressive language.
When it is all done, I find that I can get to a bug free, scalable,
flexible, object oriented result much more quickly in Python than in
Java, but YMMV.
In summary, Python has all of the capabilities of OOP, but lacks some of
the constraints of OOP. This lack of constraints has it's advantages
and disadvantages, but on the whole, I like it. What else can I say?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list