Skip to main content

28.Python Class and Instance variables

Python Class and Static variables.
    The variables defined in the  class or an object are ordinary variables that are bound to the name spaces(variables defined in the class are stored in class namespace and the variables defined with object that is within the __init__() are stored in the object namespace).This means that these variables are valid only within the scope of the classes and objects only.In the object oriented programming,the variables are of two types namely,class variables and instance(static) variables.

Instance Variable:
    The Instance variable is also known Object variables.As the name indicates,these variables are owned or belong to each individual object.They are different for different objects.In short,the variables defined within the __init__()method  are the instance variables.They are stored in the object namespace.
For Example:
The students studying in a particular school/college will have different roll numbers. ie.,each student has an individual roll number.Thus,for an object(student),the roll number is an instance variable.
Program
class Student:    #class creation
    def __init__(self,name,rollno):  #__init__() to initialize variables
        self.name=name     #instance variable
        self.rollno=rollno    #instance variable
    def message(self):
        print("Hey",self.name,",your roll no is",self.rollno)
stu1=Student("Harry",101)    #object1
stu2=Student("Ron",103)    #object2
stu1.message()
stu2.message()
Output
Hey Harry ,your roll no is 101 
Hey Ron ,your roll no is 103
Class Variable:
    Class variables are accessed by all the instances of the class.Class variables are also known as static variable.The changes in this variable are applicable to all the instances of that class.It is declared within the class but outside the __init__() method.Unlike,instance variable,it is common to all the objects.These variables are stored in class namespace.
For example:
The students studying in the college will have different roll no but same college name.Thus,every object(student) has a same class variable(College name).
Program
class Student:    #class creation
    college="ABC College"#class variable
    def __init__(self,name,rollno):  #__init__() to initialize variables
        self.name=name     #instance variable
        self.rollno=rollno    #instance variable
    def message(self):
        print("Hey",self.name,",your roll no is",self.rollno)
        print(self.name,"is studying in",self.college)
stu1=Student("Harry",101)    #object1
stu2=Student("Ron",103)    #object2
stu1.message()
stu2.message()
Output
Hey Harry ,your roll no is 101 
Harry is studying in ABC College 
Hey Ron ,your roll no is 103 
Ron is studying in ABC College
If you are interested to learn more about python programming,check out the link below!!
Happy Learning!!

Comments

  1. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.


    best institute for python in hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl