Python Forum
inheritance - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: inheritance (/thread-7415.html)



inheritance - Arifattal - Jan-09-2018

starting with python here, and can't seem to use the inheritance properly.
I am aiming to use the user input for 'x' inside class first() inside class second(),
but the information doesn't follow past.
would love some help on this one, thanks!!

class first():
    def num1(self):
        x = int(input("whats your first number?\n"))
        print(x)

class second(first):
    def num2(self):
        z = x + 2
        print(z)



both_classes = first()
both_classes = second()
both_classes.num1()
both_classes.num2()



RE: inheritance - snippsat - Jan-09-2018

self act like a transportation between between class/method internally and in a inheritance setup.
I have give example better names,so it kind of make sense.
class First():
    def number_input(self):
        self.x = int(input("whats your first number?\n"))

class Second(First):
    def result(self):
        z = self.x + 2
        print(z)
Test:
Output:
>>> obj = Second() >>> obj.number_input() whats your first number? 50 >>> obj.result() 52



RE: inheritance - Arifattal - Jan-09-2018

Brilliant, thanks so much for that snippsat, really made stuff clear.


RE: inheritance - Arifattal - Jan-15-2018

keeping on with the question, why wont this code work for me using multiple inheritance:
class First():
    def number_input(self):
        self.x = int(input("whats your first number?\n"))

class Second(First):
    def result(self):
        self.z = self.x + 2
        print(self.z)

class Third(First, Second):
    def last_result(self):
        w = self.z + self.x
        print(w)

obj = Third()
obj.number_input()
obj.result()
obj.last_result()

never mind, understood i dont need to list the first class inside the second