OOP only in modules

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Apr 10 15:33:22 CEST 2011

On Sun, 10 Apr 2011 03:35:48 -0700, newpyth wrote:

> Hi all,
> from the subject of my post, you can see I do not like very much OOP...
> and I am not the only one... Knowing that python is intrinsecally OO, I
> propose to move all OOP stuff (classes, instances and so on) to modules.

Python is based on objects, but it is not an object-oriented language. It 
is a multi-paradigm language: it includes elements of OOP, functional, 
procedural and imperative programming. Some of these are fundamental to 
Python: the "import" statement is pure imperative style.

For example, Python has:

import module  # imperative style
len(mylist)  # procedural
map(func, sequence)  # functional
mylist.sort()  # object-oriented

With a third-party package, Pyke, you can use Prolog-style logic 


(albeit with a procedural syntax). There are probably third-party 
packages for agent-based programming as well.

If you don't like OOP, you can write your code using a functional style, 
or a procedural style. List comprehensions and generator expressions are 
*very* common in Python, which come from functional and pipeline styles 
of programming.

If you really, really hate OOP, you can even write your own wrappers for 
Python objects.

Unlike Java, Python encourages by example the use of shallow class 
hierarchies. Most Python classes are only two levels deep:

+-- list
+-- tuple
+-- dict
+-- set

instead of the deep, complex hierarchies beloved by some OOP languages:

+-- Collection
    +-- Sequence
    |   +-- MutableSequence
    |   |   +-- IndexableMutableSequence
    |   |       +-- SortableIndexableMutableSequence
    |   |           +-- SortableIndexableMutableSequenceArray
    |   |               +-- List
    |   +-- ImmutableSequence
    |       +-- IndexableImmutableSequence
    |           +-- SortableIndexableImmutableSequence
    |               +-- SortableIndexableImmutableSequenceArray
    |                   +-- Tuple
    +-- Mapping

So while everything in Python is an objects, the language itself is only 
partly object oriented, and it rarely gets in the way. The OO aspect of 
Python is mostly syntax and namespaces.


More information about the Python-list mailing list