Python Programming

Class Object

Python program of class object and constructor

8/2/2021
0 views
class-object.pyPython
#!/usr/bin/evn python

# Define a class as 'student'
class student:
    
    # Constructor with parameters
    # The first argument of constructor reference to the current instance of the class
    def __init__ (self, name, science, history, math):
        self.name = name
        self.marks = science + history + math
    
    # Method of the class
    def displayMarks(self):
        print("Total marks of", self.name, "=", self.marks)
        
# 'stu1' & 'stu2' are the object of the class 'student'
stu1 = student("James", 50,60,80)
stu2 = student("Jonh", 80,50,20)

# Calling method of the class 'student'
stu1.displayMarks()
stu2.displayMarks()
pythonclass objectconstructoroops in python

Related Examples