Instance & class variables

class Employee:  # class
no_of_leaves = 8 # class variables #this will be same for all objects
pass


harish = Employee() # here harish and shiva are objects of class "Employee"
harish.name = "harish" # instance variables
harish.role = "front end"
harish.salary = 45000
print(harish.__dict__) # this will show whole instance with value as a dictionary

shiva = Employee()
shiva.name = "shiva"
shiva.role = "back end"
shiva.salary = 50000

# u can access no_of_leaves with any objects, result will be same
print(shiva.no_of_leaves)
print(harish.no_of_leaves)

# u can access from class also
print(Employee.no_of_leaves)

# u cannot change the value of no_of_leaves with objects..it is possible with only class that is Employee
Employee.no_of_leaves = 10
print(Employee.no_of_leaves)

print(Employee.__dict__)

Comments

Popular posts from this blog

Steps taken by a compiler to execute a C program

Tokens in C

Variables and Data Types in C