[Tutor] A beginner having problems with inheritance

Alan Gauld alan.gauld at btinternet.com
Tue Jul 1 20:38:35 CEST 2014


On 01/07/14 18:04, Sydney Shall wrote:

> But with more complex classes, I find that inheritance seems to work for
> simple variables, but I cannot get it to work with lists or arrays.
> Specifically my problem is to access a numpy array in the parent class,
> from within the derived class.

Caveat: I know nothing about numpy or Scypy...

> But I thought, perhaps wrongly, that with direct inheritance one does
> not need instances.

You usually need instances for anything to work with objects. The 
instances are the objects. Classes are just templates that don't 
normally do very much of anything until you create instances.
If you write a class that you never instantiate then you should probably 
have written a module instead.

> If one needs instances, how does one create them so that they are
> correctly recognised.

anInstance = TheClass()

Beyond that, you need to explain your question in more detail.
What happens when you try to create an instance? In what way
is it not "correctly recognised"?

> Perhaps, I need to read more about inheritance,

Perhaps. Inheritance can be approached from several directions:

1) Its a way to extend or change the functions of an existing class

2) Its a way to save writing code(potentially dangerous assumption)

3) Its a way to access functions in another class (maybe you
should use delegation instead?)

4) Its purely an implementation trick to provide polymorphic
behaviour in a programming language.

5) Its a logical representation of family subtypes whereby
each subtype has a "kind of" relationship to its parent.
It is more about abstract models of reality than about coding.


All of the above are true to some degree.
Which of those views do you subscribe to? Which of those
aspects would you like to know more about?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list