is python Object oriented??

Michael Torrie torriem at gmail.com
Fri Jan 30 21:14:40 CET 2009


Hung Vo wrote:
> I'm new to Python and also wondering about OOP in Python.
> 
> I want to justify the above question (is Python Object-Oriented?).
> Does Python follow the concepts/practices of Encapsulation,
> Polymorphism and Interface, which are quite familiar to Java
> programmers?

I'd say that actually Python uses encapsulation extensively throughout
the language.   Every object in Python has attributes.  Thus every
object encapsulates (contains) attributes (which are themselves objects).

I think the term "encapsulation" is often misinterpreted by some (Java
programmers in particular) to mean some kind of enforced black-box
methodology.  In effect they feel that getters and setters is the
definition of encapsulation.  This is really not true, especially if you
go back to the original OO languages, such as Smalltalk.

So if you are asking, does python enforce some kind of bizarre black box
 access semantics (requiring getters and setters), the answer is an
emphatic "no!"

Interfaces have nothing to do with OO programming as a matter of a
fundamental principle. Interfaces exist in Java to compensate for
flaws/features in the language.  Particularly the lack of multiple
inheritance which is a blessing/curse in Java.  Python just doesn't need
interfaces.  Protocols for communication with an object do not need to
be formally enforced.  For example the popular database API that most
python database libraries use just makes sure it implements certain
methods.  Thus it doesn't matter if I'm using mysql, postgresql, or
oracle.  I still call the object's "connect" method.  Such a breath of
fresh air compared to Java, in my opinion.  Such informality can be a
bit of a hindrance to some I guess.

After this entire thread, it's funny that people are still chiming in
saying, "so is it really OOP" after having it explained in so many ways.



More information about the Python-list mailing list