help needed with classes/inheritance

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Apr 23 05:48:12 EDT 2008


barbaros a écrit :
> Hello everybody,
> 
> I am building a code for surface meshes (triangulations for instance).
> I need to implement Body objects (bodies can be points, segments,
> triangles and so on), then a Mesh will be a collection of bodies,
> together with their neighbourhood relations.
> I also need OrientedBody objects, which consist in a body
> together with a plus or minus sign to describe its orientation.
> So, basically an OrientedBody is just a Body with an
> integer label stuck on it.
> 
> I implemented it in a very crude manner:
> ------------------------------------------
> class Body:

Unless you need compatibility with pre-2.2 Python versions, use a 
new-style class instead:

class Body(object):

>   [...]
> class OrientedBody:
>   def __init__ (self,b,orient=1):
>     # b is an already existing body
>     assert isinstance(b,Body)
>     self.base = b
>     self.orientation = orient
> -------------------------------------------
> 
> My question is: can it be done using inheritance ?

Technically, yes:

class OrientedBody(Body):
   def __init__(self, orient=1):
     Body.__init__(self)
     self.orient = 1

Now if it's the right thing to do is another question... Another 
possible design could be to have an orient attribute on the Body class 
itself, with 0 => non-oriented (default), 1 => 'plus', -1 => 'minus' (or 
any other convention, depending on how you use this attribute).




More information about the Python-list mailing list