When I need classes?

Michael Torrie torriem at gmail.com
Mon Jan 11 18:59:11 EST 2016


On 01/11/2016 04:45 PM, Travis Griggs wrote:
> As a long term OO purist practitioner, I would add to this.
> Obviously, you can organize your code any way you want, with or
> without classes. You could put all your functions with an odd number
> of letters in one class, and all of the even numbered ones in another
> class.

And of course in Java you have to use classes for namespaces and
organization.  In python, the equivalent is simply a module.  It's
essentially a singleton object without having to do any boiler plate.  A
module can even keep state, so long as you only need to keep track of
one instance or set of states at a time.

> Having listened to the guy (Alan Kay) who coined the term (Object
> Oriented Programming) quite a bit over the years, I believe that the
> focus of OO (of which classes are a particular implementation
> approach) is to bind behavior to data. In “traditional” programming
> approaches, one focused on the algorithm (behavior) first, and then
> figured out what data needed to flow where to get the job done.
> Classes provided a mechanism to turn that equation, generally
> speaking, around. One thinks about the data first, and then figures
> out what behavior binds best to that data. And how that data will
> interact (inter-object behavior, often called messages) to get your
> job done. For some (many) problems, this can be a real win. And for
> some, not so much.
> 
> I think, this is often why, for a simple script, OO just kind of gets
> in the way. You have a straightforward procedure that you just want
> to do. The state (data) is not rich enough to make making it the
> focal point of your program.

The beauty of Python is that you can program procedurally, and still
work with objects as needed.  Every data type in Python is some kind of
object and you can call appropriate methods on those objects.  Even if
you do no OOP programming yourself.  For example,

some_string = "foo:bar"
(a,b) = some_string.split(':')

The ease with which Python can be used in many programming paradigms is
one reason I like Python so much.



More information about the Python-list mailing list