[Tutor] Usefulness of classes and necessity of inheriting classes

Reuben reuben.dlink at gmail.com
Mon Nov 25 12:32:21 CET 2013


Thanks to Alan and Dominik for their  explanation and valuable time to
provide their inputs.


On Mon, Nov 25, 2013 at 4:41 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 25/11/13 06:37, Reuben wrote:
>
>  Question no 1:
>> ----------------------
>> I would like to know why do we actually inherit classes? What would be
>> the benefit of inheriting?
>>
>
> Inheritance is the key to providing polymorphism. It also saves a lot of
> duplication in that a new class only has to implement those methods that
> differ from the ones in the inherited class.
>
> See the BankAccount examples in my OOP tutorial topic for examples of this.
>
>
>  If possible, a practical example would be of great help
>>
>
> Real world examples include widgets in a GUI.
> You can have a Button widget which does all the standard button type
> things.
> You might then want a special button that flashes while an operation is in
> progress. You therefore define a new button class inheriting from the
> regular button. You only need to implement the display code, the code that
> responds to mouse clicks etc is all inherited from the standard.
>
> Another case in in Network management. Network management systems
> use what is called a MIB. A Management Information Base. The MIB
> is usually defined in terms of Managed Objects(MO). There is a
> standard protocol (a set of methods or API) that all MOs must
> adhere to. Specific types of network  elements (routers,
> switches, printers etc) all have their own specialist
> methods/features on top of the standard MO protocol. Specific
> models of router or printer will then have their own
> proprietary features on top of that again. So a MIB will
> typically have a deep inheritance stricture starting with MO,
> then a layer of generic devices(router, switch, printer etc)
> then a third layer of manufacturers models (eg. Cisco5300,
> HP Deskjet 4550, etc)
>
> When we come to add a new model of printer, say, to the MIB
> we want to minimize the coding so we inherit the generic printer
> object which will give us the generic MO protocol methods
> plus the generic printer features (out of paper/ink alarms etc)
> for free. We then implement the special features of that model (blue-tooth
> connection opened, banner printing mode selected
> etc). By only having to code the differences it is much easier
> to add new objects.
>
>
>  Question no 2:
>> ----------------------
>>
>> Why would I ever use a class? I understand this is strange question
>>
>> May be an example to make me understand would be useful.
>>
>> Or may be answering the question should be rephrased as "Why not use
>> modules instead of classes?"
>>
>
> Modules only allow for one instance. All the data fields in a module are
> shared between users. A class is like a new data type. You can have as many
> instances as you like and each one will have its own attributes.
>
> Think of the GUI Button class. A typical GUI screen with have several
> Buttons ('Save', "Don't Save", "Cancel" for example) Each of those buttons
> has a different piece of code linked to it and a different label, but they
> are all  Buttons. If we just had a Button module we would need to hold all
> the data about each button locally in our code and then pass all of it into
> each function every time we used it. (And that includes things like screen
> location, size, font, colors, borders, state, etc etc) It's perfectly
> possible to do that (it's what we did before OOP became popular) but it's
> very tedious, and error prone.
>
> And of course you can't inherit from Modules!
>
> However, it is important to say that you don't need to use classes.
> They are powerful tools, especially where you need to reuse code across
> projects and in organizing larger projects. But Python modules do a lot of
> the same things and in smaller projects are often sufficient.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131125/3cba7667/attachment.html>


More information about the Tutor mailing list