1. Static variables:
=================================
If the value of a variable is not varied from object to object, such type of
variables we have to declare with in the class directly but outside of methods.
Such type of variables are called Static variables.
For total class only one copy of static variable will be created and shared by
all objects of that class.
We can access static variables either by class name or by object reference.
But recommended to use class name.
Instance Variable vs Static Variable:
-------------------------------------------
Note: In the case of instance variables for every object a seperate copy will
be created,but in the case of static variables for total class only one copy
will be created and shared by every object of that class.
Various places to declare static variables:
==================================================
1. In general we can declare within the class directly but from out side of
any method
2. Inside constructor by using class name
3. Inside instance method by using class name
4. Inside classmethod by using either class name or cls variable
5. Inside static method by using class name
How to access static variables:
-------------------------------------
1. inside constructor: by using either self or classname
2. inside instance method: by using either self or classname
3. inside class method: by using either cls variable or classname
4. inside static method: by using classname
5. From outside of class: by using either object reference or classname
Where we can modify the value of static variable:
--------------------------------------------------------
Anywhere either with in the class or outside of class we can modify by using classname.
But inside class method, by using cls variable.
What If we change the value of static variable by using either self or object reference variable:
============================================================
If we change the value of static variable by using either self or object reference variable, then the value of static variable won't be changed,
just a new instance variable with that name will be added to that particular object.
How to delete static variables of a class:
===============================================
We can delete static variables from anywhere by using the following syntax
del classname.variablename
But inside classmethod we can also use cls variable
del cls.variablename
Note: By using object reference variable/self we can read static variables,
but we cannot modify or delete.
If we are trying to modify, then a new instance variable will be added to that
particular object.
t1.a = 70
If we are trying to delete then we will get error.
We can modify or delete static variables only by using classname or cls variable.
На этой странице сайта вы можете посмотреть видео онлайн Python Static Variables длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Raj Tech Solutions 29 Январь 2020, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 62 раз и оно понравилось 6 зрителям. Приятного просмотра!