Classic OOP in Python

Ned Batchelder ned at nedbatchelder.com
Thu Jun 18 07:54:10 EDT 2015


On Thursday, June 18, 2015 at 7:21:29 AM UTC-4, Jason P. wrote:
> El miércoles, 17 de junio de 2015, 21:44:51 (UTC+2), Ned Batchelder  escribió:
> > On Wednesday, June 17, 2015 at 3:21:32 PM UTC-4, Jason P. wrote:
> > > Hello Python community.
> > > 
> > > I come from a classic background in what refers to OOP. Mostly Java and PHP (> 5.3). I'm used to abstract classes, interfaces, access modifiers and so on.
> > > 
> > > Don't get me wrong. I know that despite the differences Python is fully object oriented. My point is, do you know any book or resource that explains in deep the pythonic way of doing OOP?
> > > 
> > > For example, I'm gonna try to develop a modest application from ground up using TDD. If it had been done in Java for instance, I would made extensive use of interfaces to define the boundaries of my system. How would I do something like that in Python?
> > > 
> > > 
> > > Many thanks!
> > 
> > What other languages do with interfaces, Python does with duck-typing. You
> > can build something like interfaces in Python, but many people don't bother.
> > 
> > I don't know if your project will be web-based, but here is an entire book
> > about developing Python web sites with a TDD approach:
> > 
> > http://www.obeythetestinggoat.com/
> > 
> > (Don't mind the unusual domain name, it's a bit of an inside joke...)
> > 
> > TDD and interfaces are separate concepts, and I'm not sure they even
> > intersect.  TDD is about writing tests as a way to design the best system,
> > and putting testing at the center of your development workflow.  It works
> > great with Python even without interfaces.
> > 
> > --Ned.
> 
> I'm aware of duck typing. The point in using interfaces is to be explicit about the boundaries of a system.
> 
> Quite a red "Growing Object-Oriented Software, Guided by Tests", by the way. In fact interfaces are key components in the style of building software they propose, in good company with TDD.

Yes, that book uses interfaces, because that book uses Java.  Different
languages offer different tools.  You can make interfaces in Python, but you
don't need to.  They aren't enforced by Python, so you aren't gaining much
other than documentation from them, so why not just use documentation?

Abstract classes provide another tool.  They can insist that you provide
implementations of abstract methods.  In my experience, it is easy to get
to a point where you are struggling to satisfy your simple-minded abstract
classes, rather than writing the code that you know you need to solve your
actual problem.  As Chris just mentioned elsewhere in this thread, you have
to be very careful how you define your abstract classes.  I've worked in 
Java projects where I had to provide 10 dummy implementations of methods I
knew I wasn't going to need, just because the interface insisted they had
to exist.

The Python culture is to document your expectations, and write enough tests
to verify that your code does what it claims to do.  You are already planning
on a TDD flow, so you will have plenty of tests.

Try doing without interfaces or abstract classes.  See how it goes. It's the
Python way. :)

--Ned.



More information about the Python-list mailing list