Python class variable

student.py
class Student:

 totalStudents=0
 schoolName="XYZ"

 def __init__(self,name,rollnum):
  self.name=name
  self.rollnum=rollnum
  Student.totalStudents+=1

 def show(self):
 
  print("Name:",self.name,"-","Roll No:",self.rollnum)

s1=Student("Julia",21)
s2=Student("Ema",22)
s3=Student("Megan",23)
s4=Student("Miley",24)

print("School : ",Student.schoolName)
print()
print("Students listing...")
print()
s1.show()
s2.show()
s3.show()
s4.show()

print()
print ("Total Students :",Student.totalStudents)
Output
classvar

 

Published by Python programming examples for beginners

Abhay Gadkari is an IT professional having around experience of 20+ years in IT industry. He worked on web technologies and databases with Insurance and ERP projects.