Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Example of inheritance and constructor in subclass

class Polygon:
    
    #Constructor
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]
        
    #Take user input for side lenghts    
    def inputSides(self):
        self.sides = [int(input("Enter Side: ")) for i in range(self.n)]
        
    #Print the sides of the polygon
    def displaySides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])
            
class Triangle(Polygon):
    
    def __init__(self):
        #Calling constructor of superclass
        Polygon.__init__(self, 3)
        
    def findArea(self):
        a, b, c = self.sides
        #Calculate the semi-perimeter
        s = (a + b + c) / 2
        area = (s * (s-a) * (s-b) * (s-c)) ** 0.5
        print('The area of the triangle is %0.2f' %area)
Comment

PREVIOUS NEXT
Code Example
Python :: change tag name using beautifulsoup python 
Python :: set_debug 
Python :: python pod status phase 
Python :: Get First From Table Django 
Python :: Call a function after every x seconds 
Python :: clear list in python 
Python :: list of bad words python 
Python :: enumerate count 
Python :: geopandas bbox 
Python :: Analyzing code samples, comparing more than 2 numbers 
Python :: benifits fo nested classes in python 
Python :: python basic programs area caluclation 
Python :: Using iterable unpacking operator * with extend 
Python :: django orm filter equal insensitive 
Python :: hash tables in python 
Python :: how a 16 mp camera looks like 
Python :: math plotlib 2 y axes 
Python :: filter titlecase django 
Python :: mylist = [“hello”, “bye”,”see ya”,”later”] phrase = mylist[1] 
Python :: gym for creating simple grid world 
Python :: hidden semi markov model python from scratch 
Python :: ldap python how to print entries 
Python :: even number list generator 
Python :: python insert text in string before certain symbol 
Python :: Center labels matplotlib histogram 
Python :: numpy addition operation using numpy functions 
Python :: region python 
Python :: django save object 
Python :: tutorial on how to search the database in your django project 
Python :: th most effective search methods in python with example 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =