Instances behaviour

Mike Meyer mwm at mired.org
Thu Dec 1 20:17:58 EST 2005


"Mr.Rech" <andrea.riciputi at gmail.com> writes:
> 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:
>
> 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.
>
> Perhaps some OOP techniques (that I miss at the moment) could be of any
> help. Any suggestion?

I assume there are methods of B & C that aren't shared, and hence
aren't in A. When the user invokes those, they should get an error
message. That's how this kind of thing is normally dealt with.

If you want things to happen at instantiation time, then you can make
A.__init__ raise an exception. Your B & C __init__ then can't invoke
it. If A.__init__ has a real use, move that into another method that B
& C's __init__ can invoke.

   <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list