Object Oriented vs Pythonic Code, and Pythonic standards

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Feb 7 17:15:39 EST 2006


Carl J. Van Arsdall a écrit :
> It seems the more I come to learn about Python as a langauge and the way 
> its used I've come across several discussions where people discuss how 
> to do things using an OO model and then how to design software in a more 
> "Pythonic" way.

Well, Python being mostly OO (about 101% I'd say), doing things "the 
Pythonic way" and doing things "the OO way" often overlap.

> My question is, should we as python developers be trying to write code 
> that follows more of a python standard

Yes - given that this "standard" is a moving target (as Python grows and 
changes, so does pythonic idioms).

> or should we try to spend our 
> efforts to stick to a more traditional OO model?

Depends on what you call a "traditional OO model" !-)

> For example, in C++ I might make a file that defines a class and all its 
> methods, at which point I create an object and do things with it.  My 
> interpretation of what is "Pythonic" would be instead of creating a 
> class I would just define functions and maybe some variables global to a 
> module.  At this point, I import the module and just make function calls.

I think you should have a closer look at the standard lib. I did not run 
an exhaustive analysis, but it seems to me that it's mostly built around 
classes.

> There are many similarities here, but the difference is that in python I 
> don't feel as though I would define a class, I would just treat the 
> python module as a class instead, especially if it was a type of object 
> that I would only need a single instance of.

Strange enough, that's *exactly* what is it. Python modules *are* 
instances of class module - like functions are instances of class 
function, and even classes are themselves instance of their metaclasses...

I'm afraid that years of C++/Java/Ada/C# and other class-based, 
statically typed languages domination results in (too) many programmers 
confusing *object* oriented with *class* oriented.

As an example, most of the functional programming support in Python 
comes from the fact that functions are objects too (which is the first 
and mandatory requirement for fp) and that any class can also become a 
new 'type' of function. So, are Python's function decorators 
'functional' or 'oo' ? HigherOrderFunctions are of course typical of fp, 
but the way they work in Python is mostly OO (yes, this is the good old
Decorator pattern, applied to function objects...)

> The second question that arises from Pythonism is, has the community 
> drafted a standard for quality "Pythonic" code?

launch your Python interactive interpreter and type "import this"...




More information about the Python-list mailing list