help needed with classes/inheritance

Andrew Lee fiacre.patrick at gmail.com
Wed Apr 23 06:49:31 EDT 2008


barbaros wrote:
> 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:
>   [...]
> class OrientedBody:
>   def __init__ (self,b,orient=1):
>     # b is an already existing body
>     assert isinstance(b,Body)
>     self.base = b
>     self.orientation = orient
> -------------------------------------------

class Body(object) :
	...

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



as noted


But, also.

as a rule of thumb .. if you are using "isinstance" in a class to 
determine what class a parameter is ... you have broken the OO contract. 
  Remember, every class ought to have a well defined internal state and 
a well defined interface to its state.

If I write --

class foo (object):
    def __init__ :
       pass

    def some_func (self, val) :
       if isinstance (val, "bar") :
	....

Then I am either doing something very wrong or very clever (either can 
get me in trouble)

In Python it is preferred that I write two functions some_func_a and 
some_func_b

e.g.

     def some_func_a (self, val = None, class = bar) :
         assert(isinstance (class, "bar"), True)
         ....

     def some_func_b (self, val = None, class = baz) :
         assert (isinstance (class, "baz"), True)

C++ and Java try to enforce the OO contract by making data and methods 
private, protected or public.  Which helps -- but leads to some 
confusion (what is protected inheritance in C++????)  Python exposes all 
of its classes internals to everyone -- but that doesn't mean you should 
touch them!!

As Larry Wall once wrote, "There is a difference between, 'do not enter 
my living room because I asked you not to' and 'do not enter my living 
room because I have a shotgun'"

Python adopts the 'do not touch my private parts because I asked you not 
to' idiom.  (In C++, only friends can touch your privates ... ;-)

So -- be mindful that checking the class of well defined parameters at 
anytime is breaking the contract -- you may need to do it -- but it is 
more likely that you aren't adhering to good OOD.

Does that make any sense?

Seriously -- I have not had any coffee yet and I am still new at Python.


-- Andrew




> 
> My question is: can it be done using inheritance ?
> I recall that I need three distinct objects:
> the basic (non-oriented) body, the same body with positive
> orientation and the same body with negative orientation.
> 
> Thank you. Cristian Barbarosie
> http://cmaf.fc.ul.pt/~barbaros



More information about the Python-list mailing list