Inheritance

Base - Derived

In [7]:
# Python example to check if a class is 
# subclass of another 

class Base: 
	pass # Empty Class 

class Derived(Base): 
	pass # Empty Class 
In [8]:
# Driver Code 
print(issubclass(Derived, Base)) 
print(issubclass(Base, Derived)) 
True
False
In [9]:
d = Derived() 
b = Base() 
In [10]:
# b is not an instance of Derived 
print(isinstance(b, Derived)) 
False
In [11]:
# But d is an instance of Base 
print(isinstance(d, Base)) 
print(isinstance(d, Derived))
True
True
In [6]:
# Python example to show that base 
# class members can be accessed in 
# derived class using base class name 
class Base(object): 

	# Constructor 
	def __init__(self, x): 
		self.x = x	 

class Derived(Base): 

	# Constructor 
	def __init__(self, x, y): 
		Base.x = x 
		self.y = y 

	def printXY(self): 
		# print(self.x, self.y) will also work 
		print(Base.x, self.y) 


# Driver Code 
d = Derived(10, 20) 
d.printXY() 
10 20

Person - Employee

In [7]:
# A Sample class with init method 
class Person: 
	# init method or constructor 
	def __init__(self, name): 
		self.name = name 

	# Sample Method 
	def say_hi(self): 
		print('Hello, my name is', self.name) 
In [8]:
p = Person('Ahmed') 
p.say_hi() 
Hello, my name is Ahmed
In [9]:
class Person:
    def __init__(self, first, last, age):
        self.firstname = first
        self.lastname = last
        self.age = age

    def __str__(self):
        #return self.firstname + " " + self.lastname + ", " + str(self.age)
        return 'first {}\tlast{}\tage{}'.format(self.firstname, self.lastname, self.age)

class Employee(Person):
    def __init__(self, first, last, age, staffnum):
        super().__init__(first, last, age)
        self.staffnumber = staffnum

    def __str__(self):
        # return super().__str__() + ", " +  self.staffnumber
        return '{}\t{}'.format(super().__str__(), self.staffnumber)
In [10]:
p = Person("Ahmed", "Hamad", 36)
e = Employee("Ali", "Jabir", 28, "1007")

print(p)
print(e)
Ahmed Hamad, 36
Ali Jabir, 28, 1007

Person - Student

In [31]:
class Person:
    def __init__(self, first, last, age, gender, address):
        self.firstname = first
        self.lastname = last
        self.age = age
        self.gender = gender
        self.address = address

    def __str__(self):
        return self.firstname + " " + self.lastname + ", " + str(self.age)
    
    def walk(self):
        print('my name is {}. I am walking'.format((self.firstname)))

class Student(Person):
    def __init__(self, std_id, dept, first, last, age, gender, address):
        Person.__init__(first, last, age, gender, address)
        self.std_id = std_id 
        self.dept = dept
    def study(self):
        print('my name is {}. I am studying'.format((self.firstname)))
    def __str__(self):
        return self.std_id + ' ' + super().__str__() 
        
In [32]:
p1 = Person('Mohammed', 'Ahmed', 28, 'm', 'Gaza')
print(p1)
Mohammed Ahmed, 28
In [34]:
s1 = Student('120191122', 'MM', 'Ahmed', 'Ali', 20, 'm', 'Khanyounes')
print(s1)
s1.walk()
s1.study()
120191122 Ahmed Ali, 20
my name is Ahmed. I am walking
my name is Ahmed. I am studying

Rectangle - Square - Cube

In [12]:
class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * (self.length + self.width)
    
r1 = Rectangle(3,4) # rectangle
s1 = Rectangle(3,3) # square 
In [13]:
# Here we declare that the Square class inherits from the Rectangle class
class Square(Rectangle):
    def __init__(self, x):
        super().__init__(x, x)
        
s2 = Square(3)
In [14]:
class Cube(Square):
    def surface_area(self):
        face_area = super().area()
        return face_area * 6

    def volume(self):
        face_area = super().area()
        return face_area * self.length
In [15]:
square = Square(4)
square.area()
Out[15]:
16
In [16]:
cube = Cube(3)
cube.surface_area()
Out[16]:
54
In [17]:
cube.volume()
Out[17]:
27

A <- B <- C

image.png

In [18]:
class A:
    pass 

class B(A): # B is child class, A: Parent 
    pass

class C(B): # C is Child, B is parent 
    pass
In [9]:
class A:
    x = 10  

class B(A): # B is child class, A: Parent 
    y = 10 

class C(B): # C is Child, B is parent 
    z = 30 
    
In [15]:
a = A()
b = B()
c = C()
print(isinstance(a, A))
print(isinstance(a, B))
print(isinstance(b, A))
print(isinstance(b, B))
True
False
True
True
In [10]:
b = B()
print(b.x)
10
In [11]:
print(issubclass(B, A)) 
print(issubclass(A, B))
print(issubclass(C, B))
print(issubclass(C, A))
True
False
True
True
In [1]:
class X:
    pass 

class Y:
    pass 

x1 = X()
x2 = X()

y1 = Y()
y2 = Y()

print(isinstance(x1, X))
print(isinstance(x1, Y))
print(isinstance(y1, X))
True
False
False
In [14]:
def f1(x):
    return x*x


print(f1(3))
9
In [15]:
n = 10 
print(f1(n))
100
In [ ]: