Re: [Tutor] A beginner having problems with inheritance
On 09/07/2014 20:17, Danny Yoo wrote: My error was simply that I inadvertently used the same name for a method (function) in the derived class that I had already used in the parent class. The result was then a very obscure error because the wrong calculation was performed and later on an array was empty. Fortunately, thanks to you very generous tutors I have learned to read the error trace very carefully indeed. Ah, yes, that one. It's happened to me too. It's one of the reasons why inheritance makes me nervous sometimes. It's a problem that's fairly language-independent. (If you'd like to know the details, the example that I was involved in is described in the comments on: https://github.com/bootstrapworld/js-numbers/pull/5. Essentially, we subclassed some base class, and wrote a method called BigNumber.exp() to do exponentiation. Unfortunately, we didn't realize that our superclass already had defined an exp() method. It ended up showing up as a very strange, obscure error. Same class of problem.) Side note, Python does have a hack called "name mangling" that can be used to try to avoid this problem: https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references Good luck! Thanks for the reply. I had read the second reference that you gave some time ago, but of course I had forgotten about it. I shall try and make use of it now. Thanks for the help. -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A beginner having problems with inheritance
> My error was simply that I inadvertently used the same name for a method > (function) in the derived class that I had already used in the parent class. > The result was then a very obscure error because the wrong calculation was > performed and later on an array was empty. > Fortunately, thanks to you very generous tutors I have learned to read the > error trace very carefully indeed. Ah, yes, that one. It's happened to me too. It's one of the reasons why inheritance makes me nervous sometimes. It's a problem that's fairly language-independent. (If you'd like to know the details, the example that I was involved in is described in the comments on: https://github.com/bootstrapworld/js-numbers/pull/5. Essentially, we subclassed some base class, and wrote a method called BigNumber.exp() to do exponentiation. Unfortunately, we didn't realize that our superclass already had defined an exp() method. It ended up showing up as a very strange, obscure error. Same class of problem.) Side note, Python does have a hack called "name mangling" that can be used to try to avoid this problem: https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A beginner having problems with inheritance
On 06/07/2014 23:06, Danny Yoo wrote: My apologies to the tutors. I have identified my error, which was predictably elementary. With many thanks, By the way, can you say what your conceptual error was or give an example? It might help the other folks here who are learning and who may be making a similar mistake. (And I'm curious!) But if you don't want to say, that's ok too. Good luck! Thanks Danny, My error was simply that I inadvertently used the same name for a method (function) in the derived class that I had already used in the parent class. The result was then a very obscure error because the wrong calculation was performed and later on an array was empty. Fortunately, thanks to you very generous tutors I have learned to read the error trace very carefully indeed. Thanks a lot. Sydney -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A beginner having problems with inheritance
> My apologies to the tutors. > I have identified my error, which was predictably elementary. > With many thanks, By the way, can you say what your conceptual error was or give an example? It might help the other folks here who are learning and who may be making a similar mistake. (And I'm curious!) But if you don't want to say, that's ok too. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A beginner having problems with inheritance
On 01/07/2014 19:04, Sydney Shall wrote: I am a beginner and I have always had problems with inheritance. I can get inheritance to work with very simple classes. 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. When I test with instances in IPython everything works properly. But I thought, perhaps wrongly, that with direct inheritance one does not need instances. If one needs instances, how does one create them so that they are correctly recognised. Perhaps, I need to read more about inheritance, Any suggestions for reading would be welcome. With many thanks. My apologies to the tutors. I have identified my error, which was predictably elementary. With many thanks, Sydney -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A beginner having problems with inheritance
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] A beginner having problems with inheritance
I am a beginner and I have always had problems with inheritance. I can get inheritance to work with very simple classes. 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. When I test with instances in IPython everything works properly. But I thought, perhaps wrongly, that with direct inheritance one does not need instances. If one needs instances, how does one create them so that they are correctly recognised. Perhaps, I need to read more about inheritance, Any suggestions for reading would be welcome. With many thanks. -- Sydney Shall ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor