OOP techniques in Python

bruno at modulix onurb at xiludom.gro
Thu Apr 27 15:13:29 EDT 2006


Panos Laganakos wrote:
> I've been thinking if there's a point in applying some specific OOP
> techniques in Python as we do in other languages.

Yes - but some of these techniques are somewhat python-specific.

> i.e. we usually define private properties and provide public functions
> to access them, in the form of:
> get { ... } set { ... }
>
> Should we do the same in Python:

No.

> 
> Or there's no point in doing so?

Absolutely. Python has descriptors and properties, which allow you to
switch from a direct attribute access to a computed attributes without
breaking the API. In fact, think of public attributes as automatic
getter/setters pairs, that you can customize when needed !-)

> Some other techniques come to mind, but I think that Python tends to
> allow the programmer to access stuff he wants even though he shouldn't

s/he shouldn't/he knows he's messing with implementation details and
assume the consequences.

FWIW, accessing 'private' attributes in Java or C++ is not a big deal.

> or in the form of a dict or list, rather than a method to do so.

Python is *object* oriented - not class-oriented - and doesn't force you
to put everything into a class definition. Now everything in Python is
an object (including functions and classes and modules...). This is a
much less restricted (and IMHO much much more powerful) approach than
what you may learn from Java, C++, C# etc.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list