question about globals vars...

Sean 'Shaleh' Perry shalehperry at attbi.com
Fri Aug 2 22:51:48 CEST 2002


On 02-Aug-2002 Will Stuyvesant wrote:
> Just posted a reply to your questions about globals with an example of
> problems with OO programming.
> A better example is this:
> Suppose class S has methods push(x), pop() and multipop(n), with
> obvious meanings.
> Now suppose you want to code class Sn and inherit from class S and
> implement a numberOfItems attribute.  If multipop(n) calls pop() n
> times you have to override pop() so it will update numberOfItems
> correctly.  But if multipop(n) does not call pop() you would have to
> override multipop().  To decide what to do you have to look at the
> source code of class S.  In an ideal situation the documentation of S
> tells you all you need, but this is rarely the case and in the last
> example it will probably not be documented if multipop calls pop.
> 

sounds like broken inheritance and broken design to me.

Sn should contain an instance of S and calls its methods while maintaining its
own numberOfItems attribute.  You can't fix a broken design with inheritance.

This is not an OO problem.  Any time you implement data encapsulation this type
of issue comes up.  Deciding how much of an interface to provide is always
tricky.




More information about the Python-list mailing list