25
loading...
This website collects cookies to deliver better user experience
class inherited(master)
class sample():
a=2
b=3
def fun(self):
print("Hello world")
class sample2(sample):
def fun2(self):
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
mysample2.fun2()
Hello world
5
class sample2():
This syntax will create class sample2
class sample2(sample)
: This syntax will create a class sample2
and make it inherit the class sample
sample2
inherits sample
, it can use it's attributes (like here a and b) in it's body. This is why no errors were generated when we used to variables a and b directly in the function fun2
. The values fir a and b were assigned in the master class (sample2
itself ands need not be assigned again . This is an example of code reuse and information hidingsample2
can also use the functions declared in the master class. This is how the function fun
could be used and run successfully.class sample():
a=2
b=3
def __init__(self):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def fun2(self):
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
mysample2.fun2()
Hello world
5
sample2.__init__
is not the same as sample.__init__
.sample2
class. So python made a default blank constructor in it's place, which had no relation to the master class. class sample():
a=2
b=3
def __init__(self):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def __init__(self):
super().__init__()
def fun2(self):
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
mysample2.fun2()
class sample():
a=2
b=3
def __init__(self,var):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def fun2(self):
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
mysample2.fun2()
Traceback (most recent call last):
File "main.py", line 14, in <module>
mysample2=sample2()
TypeError: __init__() missing 1 required positional argument: 'var'
class sample():
a=2
b=3
def __init__(self,var):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def fun2(self):
print(self.a+self.b)
mysample2=sample2(3)
mysample2.fun()
mysample2.fun2()
Hello world
150
class sample():
a=2
b=3
def __init__(self):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def __init__(self):
a=100
b=200
def fun2(self):
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
mysample2.fun2()
Hello world
5
self.a=100
and self.b=200
should haver been written in place of a=100
and b=200
class sample():
a=2
b=3
def __init__(self):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def __init__(self):
super().__init__()
def fun(self):
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
150
class sample():
a=2
b=3
def __init__(self):
self.a=100
self.b=50
def fun(self):
print("Hello world")
class sample2(sample):
def __init__(self):
super().__init__()
def fun(self):
super().fun()
print(self.a+self.b)
mysample2=sample2()
mysample2.fun()
Hello world
150