Abstract class

Gary Herron gherron at islandtraining.com
Sun Sep 14 21:15:04 CEST 2008


Mr.SpOOn wrote:
> Gary Harron:
>   
>> I believe you are mixing up class *inheritance* and *abstract* classes.
>>     
>
>   
>> Class inheritance (with Python has has for years) is how one class inherits >behavior/properties/attributes from another class.  The class being inherited from is >called the base class.  This is probably what you want.
>>     
>
> Well, I know the difference between an abstract class and an inherited
> one. The idea was to create a main class Note, with abstract methods,
> and implement these methods in the other classes.
>
> On Sun, Sep 14, 2008 at 7:56 PM, Roy Smith <roy at panix.com> wrote:
>   
>> What properties or behaviors does SharpNote have which NaturalNote doesn't?
>> Unless there is some new behavior, you don't need subclasses.
>>     
>
> Well, from a SharpNote I can obtain the relative NaturalNote. So if I
> have a C# I can call
>
> natural('C#')  and get 'C'
>
> While in the class NaturalNote I don't need such a method, but I need
> two methods to get the sharped and flatted version
>
>   
>> Are you also going to have DoubleSharpNote and DoubleFlatNote?
>>     
>
> Yes, that's an option.
>
>   
>> Consider the following code:
>>
>> note1 = SharpNote("E4")
>> note2 = NaturalNote("F4")
>> if note1 == note2:
>>   print "the same note"
>> else
>>   print "different notes"
>>
>> what should it print?
>>     
>
> Well, that's not so simple. The idea is that I use a notation (A, B,
> C, D...) and an integer (a distance expressed in semitones) to
> identify a note.
>
> Anyway, I think I need an abstract class. Or not?
>   

No!  Definitely not!    You need inheritance of a class from a base 
class.  

Implement your base class with whatever shared methods you want. 
Implement your derived class with methods (new, inherited or 
reimplemented) as you like.
(If you wish to consider the base class "abstract", just agree with 
yourself to not instantiate it.)

Please forget about Abstract Base Classes.  They have nothing to do with 
what you want, and are a *HUGE* overkill for your application.   They 
are not (yet) even part of standard Python, and are used primarily for a 
class implementor to guarantee  to a user of a class that it provides a 
specific interface.

Gary Herron

> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list