Instances behaviour

Mr.Rech andrea.riciputi at gmail.com
Thu Dec 1 18:51:05 EST 2005


Hi all,
I've been using Python for 3 years, but I've rarely used its OOP
features (I'm a physicist, sorry). Now, after having read a lot about
Python OOP capabilities, I'm trying to get advantage of this (for me)
new paradigm. As a result I've a lot of somewhat philosophical
questions. I will start with one of them.

Suppose I have a bunch of classes that represent slightly (but
conceptually) different object. The instances of each class must behave
in very similar manner, so that I've created a common class ancestor
(let say A) that define a lot of special method (such as __getattr__,
__setattr__, __len__ and so on), and then I've created all my "real"
classes inheriting from it:

>>>class A(object):
....     # here define all special and some common methods

>>> class B(A):
....    # this is the first "real" class

>>> class C(A):
....    # and this is the second

and so on. The problem I'm worried about is that an unaware user may
create an instance of "A" supposing that it has any real use, while it
is only a sort of prototype. However, I can't see (from my limited
point of view) any other way to rearrange things and still get a
similar behaviour.

Implementing those special methods directly in class B and then inherit
from it, doesn't seem the right way, since I'd prefer that instances of
B weren't in any relation with the instances of C (i.e. I'd prefer not
to subclass C from B)

Perhaps some OOP techniques (that I miss at the moment) could be of any
help. Any suggestion?

Thanks in advance,
 Andrea.




More information about the Python-list mailing list