Object-oriented philosophy

Michael F. Stemper michael.stemper at gmail.com
Thu Sep 6 16:24:44 EDT 2018


On 2018-09-06 09:35, Rhodri James wrote:
> On 06/09/18 15:04, Michael F. Stemper wrote:
>> Net net is that the only thing that ended up being common was the
>> __init__ methods. Two of the classes have identical __init__
>> methods; the third has a superset of that method. The other methods
>> all have completely different implementations. This isn't due to
>> poor coding, but due to the fact that what these model have
>> different physical characteristics.
>>
>> Not being that familiar with object-oriented programming (I grew up
>> on FORTRAN and c), I'm seeking opinions:
>>
>> Is there really any benefit to this change? Yes, I've eliminated
>> some (a few lines per class) duplicate code. On the other hand,
>> I've added the parent class and the (probably small, but not
>> non-existent) overhead of invoking super().
> 
> What you've done is the work you would have to do in a statically-typed
> language such as C++.  You've been taking advantage of duck typing

Upon review, it seems that I haven't. However, as noted in another
followup, I think that I've found a way to do so.

> Is it worth creating the superclass in Python?  It sounds like it's a
> bit marginal in your case.  I'm not that seasoned in object-oriented
> design either, but my yardstick would be how much common code does it
> eliminate?

About half a dozen lines. Here's the common part:

def __init__( self, xmlmodel, V, id ):

  try:
    P_0s = xmlmodel.findall( 'RatedPower' )[0].text
    self.P_0 = float( P_0s )
  except:
    Utility.DataErr( "Power not specified for %s load" % (id) )
  if self.P_0<=0.0:
    Utility.DataErr( "Power for %s load must be postive, not %s" \
      % (id,P_0s) )

Actually, looking over it, I see that I can slightly simplify this.
I suppose that's a good reason for putting it in one place -- if I
want to change it, I only need to do it once, not three times.

Thanks for your input.

-- 
Michael F. Stemper
Why doesn't anybody care about apathy?



More information about the Python-list mailing list