inheritance - python call methods from super class -


i trying understand python inheritance little. here use case:

class test:      def hello():         print("hello")         return "hello"      def hi():         print(hello())         print("hi")  class test1(test):     hi()     pass  x = test1() x.hello() 

i don't understand why can't call "hi()" in class test1. inherited class test right?

i think misunderstanding relationship , definitions of classes , objects. classes blueprints create objects. writing methods inside class, defining behaviors of object can created class blueprint. so, code:

class test:      def hello():         print("hello")         return "hello"      def hi():         print(hello())         print("hi")   class test1(test):      hi() # <- shouldn't call method directly "blueprint" class     pass  x = test1() x.hello() 

while last 2 lines of code valid, it's bad idea (and invalid) call method outside of class's scope directly inside class definition. reason why hi() not work in test1 class it's not defined in scope of test1; though class indeed inherits test class, only affects object created class (like object x created test1() class).

here's more detailed explanation classes , objects relevant question: what classes, references , objects?

hope helped!


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -