python without OO

beliavsky at beliavsky at
Wed Jan 26 18:53:03 CET 2005

Peter Maas wrote:
> Davor schrieb:
> > so initially I was hoping this is all what Python is about, but
when I
> > started looking into it it has a huge amount of additional (mainly
> > stuff which makes it in my view quite bloated now.
> So you think f.write('Hello world') is bloated and
file_write(f,'Hello world')
> is not? This is the minimum amount of OO you will see when using
Python. But
> I guess you will use OO in the long run to *avoid* bloated code:
> --------------snip---------------
> print  "*** Davor's evolution towards an OO programmer ***"
> print '\n*** Step 1: OO is evil, have to use atomic variables:'
> name1 = 'Smith'
> age1 = 35
> sex1 = 'male'
> name2 = 'Miller'
> age2 = 33
> sex2 = 'female'
> print name1, age1, sex1, name2, age2, sex2
> print '\n*** Step 2: This is messy, put stuff in lists:'
> p1 = ['Smith', 35, 'male']
> p2 = ['Miller', 33, 'female']
> for e in p1:
>      print e
> for e in p2:
>      print e

Being a Fortranner, my "Step 2" would be to use parallel arrays:

names = ['Smith','Miller']
ages  = [35,33]
sexes = ['male','female']

for i in range(len(names)):
print names[i],ages[i],sexes[i]

There are disadvantages of parallel arrays (lists) -- instead of
passing one list of 'persons' to a function one must pass 3 lists, and
one can unexpectedly have lists of different sizes if there is a bug in
the program or data file. But there are advantages, too. Sometimes one
does not need to pass the entire data set to a function, just one
attribute (for examples, the ages to compute the average age). This is
more convenient with parallel arrays than a list of objects (although
it's easy in Fortran 90/95).

I am not saying that parallel arrays are better than classes for
storing data, just that they are not always worse.

It is a strength of Python that like C++, it does not force you to
program in an object-oriented style. Lutz and Ascher, in the 2nd
edition of "Learning Python", do not mention OOP for almost the first
300 pages. They say (p297)

"Python OOP is entirely optional, and you don't need to use classes
just to get started. In fact, you can get plenty of work done with
simpler constructs such as functions, or even simple top-level script
code. But classes turn out to be one of the most useful tools Python
provides ..."

More information about the Python-list mailing list