Nội dung chính
- 1 Xem The set of all the values or contents of a class object’s instance variables 2024
- 2 Table of contents
- 3 What is an Instance Variable in Python?
- 4 Create Instance Variables
- 5 Modify Values of Instance Variables
- 6 Ways to Access Instance Variable
- 7 Instance Variables Naming Conventions
- 8 Dynamically Add Instance Variable to a Object
- 9 Dynamically Delete Instance Variable
- 10 Access Instance Variable From Another Class
- 11 List all Instance Variables of a Object
Xem The set of all the values or contents of a class object’s instance variables 2024
In Object-oriented programming, when we design a class, we use instance variables and class variables.
- Instance variables: If the value of a variable varies from object to object, then such variables are called instance variables.
- Class Variables: A class variable is a variable that is declared inside of class, but outside of any instance method or
class Student: # constructor def __init__(self, name, age): # Instance variable self.name = name self.age = age # create object stud = Student("Jessa", 20) print('Before') print('Name:', stud.name, 'Age:', stud.age) # modify instance variable stud.name = 'Emma' stud.age = 15 print('After') print('Name:', stud.name, 'Age:', stud.age)
1 method.
After reading this article, you’ll learn:
- How to create and access instance variables
- Modify values of instance variables
- How to dynamically add or delete instance variables
- Scope of a instance variables
Table of contents
What is an Instance Variable in Python?
If the value of a variable varies from object to object, then such variables are called instance variables. For every object, a separate copy of the instance variable will be created.
Instance variables are not shared by objects. Every object has its own copy of the instance attribute. This means that for each object of a class, the instance variable value is different.
When we create classes in Python, instance methods are used regularly. we need to create an object to execute the block of code or action defined in the instance method.
Instance variables are used within the instance method. We use the instance method to perform a set of actions on the data/value provided by the instance variable.
We can access the instance variable using the object and dot (
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
2) operator.
In Python, to work with an instance variable and method, we use the
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
3 keyword. We use the
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
3 keyword as the first parameter to a method. The
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
3 refers to the current object.
Declare Instance variable
Create Instance Variables
Instance variables are declared inside a method using the
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
3 keyword. We use a constructor to define and initialize the instance variables. Let’s see the example to declare an instance variable in Python.
Example:
In the following example, we are creating two instance variable
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
7 and
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
8 in the
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
9 class.
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create first object
s1 = Student("Jessa", 20)
# access instance variable
print('Object 1')
print('Name:', s1.name)
print('Age:', s1.age)
# create second object
s2= Student("Kelly", 10)
# access instance variable
print('Object 2')
print('Name:', s2.name)
print('Age:', s2.age)
Output
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
Note:
- When we created an object, we passed the values to the instance variables using a constructor.
- Each object contains different values because we passed different values to a constructor to initialize the object.
- Variable declared outside
class Student: # constructor def __init__(self, name, age): # Instance variable self.name = name self.age = age # create object stud = Student("Jessa", 20) print('Before') print('Name:', stud.name, 'Age:', stud.age) # modify instance variable stud.name = 'Emma' stud.age = 15 print('After') print('Name:', stud.name, 'Age:', stud.age)
1 belong to the class. They’re shared by all instances.
Modify Values of Instance Variables
We can modify the value of the instance variable and assign a new value to it using the object reference.
Note: When you change the instance variable’s values of one object, the changes will not be reflected in the remaining objects because every object maintains a separate copy of the instance variable.
Example
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
print('Before')
print('Name:', stud.name, 'Age:', stud.age)
# modify instance variable
stud.name = 'Emma'
stud.age = 15
print('After')
print('Name:', stud.name, 'Age:', stud.age)
Output
Before Name: Jessa Age: 20 After Name: Emma Age: 15
Ways to Access Instance Variable
There are two ways to access the instance variable of class:
- Within the class in instance method by using the object reference (
class Student: # constructor def __init__(self, name, age): # Instance variable self.name = name self.age = age # create object stud = Student("Jessa", 20) print('Before') print('Name:', stud.name, 'Age:', stud.age) # modify instance variable stud.name = 'Emma' stud.age = 15 print('After') print('Name:', stud.name, 'Age:', stud.age)
3)
- Using
Before Name: Jessa Age: 20 After Name: Emma Age: 15
2 method
Example 1: Access instance variable in the instance method
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# instance method access instance variable
def show(self):
print('Name:', stud.name, 'Age:', stud.age)
# create object
stud = Student("Jessa", 20)
# call instance method
stud.show()
Output
Name: Jessa Age: 20
instance variables and methods
Example 2: Access instance variable using
Before Name: Jessa Age: 20 After Name: Emma Age: 15
2
getattr(Object, 'instance_variable')
Pass the object reference and instance variable name to the
Before Name: Jessa Age: 20 After Name: Emma Age: 15
2 method to get the value of an instance variable.
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# create object
stud = Student("Jessa", 20)
# Use getattr instead of stud.name
print('Name:', getattr(stud, 'name'))
print('Age:', getattr(stud, 'age'))
Output
Name: Jessa Age: 20
Instance Variables Naming Conventions
- Instance variable names should be all lower case. For example,
Before Name: Jessa Age: 20 After Name: Emma Age: 15
5
- Words in an instance variable name should be separated by an underscore. For example,
Before Name: Jessa Age: 20 After Name: Emma Age: 15
6
- Non-public instance variables should begin with a single underscore
- If an instance name needs to be mangled, two underscores may begin its name
Dynamically Add Instance Variable to a Object
We can add instance variables from the outside of class to a particular object. Use the following syntax to add the new instance variable to the object.
object_referance.variable_name = value
Example:
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
0
Output
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
1
Note:
- We cannot add an instance variable to a class from outside because instance variables belong to objects.
- Adding an instance variable to one object will not be reflected the remaining objects because every object has a separate copy of the instance variable.
Dynamically Delete Instance Variable
In Python, we use the
Before Name: Jessa Age: 20 After Name: Emma Age: 15
7 statement and
Before Name: Jessa Age: 20 After Name: Emma Age: 15
8 function to delete the attribute of an object. Both of them do the same thing.
Before Name: Jessa Age: 20 After Name: Emma Age: 15
7 statement: The
Before Name: Jessa Age: 20 After Name: Emma Age: 15
7 keyword is used to delete objects. In Python, everything is an object, so the
Before Name: Jessa Age: 20 After Name: Emma Age: 15
7 keyword can also be used to delete variables, lists, or parts of a list, etc.
Before Name: Jessa Age: 20 After Name: Emma Age: 15
8 function: Used to delete an instance variable dynamically.
Note: When we try to access the deleted attribute, it raises an attribute error.
Example 1: Using the
Before Name: Jessa Age: 20 After Name: Emma Age: 15
7 statement
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
2
Output
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
3
Before Name: Jessa Age: 20 After Name: Emma Age: 15
8 function
The delattr() function is used to delete the named attribute from the object with the prior permission of the object. Use the following syntax.
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
4
class Student: # constructor def __init__(self, name, age): # Instance variable self.name = name self.age = age # instance method access instance variable def show(self): print('Name:', stud.name, 'Age:', stud.age) # create object stud = Student("Jessa", 20) # call instance method stud.show()
5: the object whose attribute we want to delete.
class Student: # constructor def __init__(self, name, age): # Instance variable self.name = name self.age = age # instance method access instance variable def show(self): print('Name:', stud.name, 'Age:', stud.age) # create object stud = Student("Jessa", 20) # call instance method stud.show()
6: the name of the instance variable we want to delete from the object.
Example
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
5
Output
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
6
Access Instance Variable From Another Class
We can access instance variables of one class from another class using object reference. It is useful when we implement the concept of inheritance in Python, and we want to access the parent class instance variable from a child class.
let’s understand this with the help of an example.
In this example, the
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# instance method access instance variable
def show(self):
print('Name:', stud.name, 'Age:', stud.age)
# create object
stud = Student("Jessa", 20)
# call instance method
stud.show()
7 is an instance variable of the
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# instance method access instance variable
def show(self):
print('Name:', stud.name, 'Age:', stud.age)
# create object
stud = Student("Jessa", 20)
# call instance method
stud.show()
8 class. We inherited a
class Student:
# constructor
def __init__(self, name, age):
# Instance variable
self.name = name
self.age = age
# instance method access instance variable
def show(self):
print('Name:', stud.name, 'Age:', stud.age)
# create object
stud = Student("Jessa", 20)
# call instance method
stud.show()
8 class to access its instance variables in
Name: Jessa Age: 20
0 class
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
7
Output
Object 1 Name: Jessa Age: 20 Object 2 Name: Kelly Age: 10
8
List all Instance Variables of a Object
We can get the list of all the instance variables the object has. Use the
Name: Jessa Age: 20
1 function of an object to get all instance variables along with their value.
The
Name: Jessa Age: 20
1 function returns a dictionary that contains variable name as a key and variable value as a value
What is the instance of a class called?
What is an object instance of a class?
What are class object variables?
What is an instance variable in Java?
Bạn đang tìm hiểu bài viết: The set of all the values or contents of a class object’s instance variables 2024
HỆ THỐNG CỬA HÀNG TRÙM SỈ QUẢNG CHÂU
Điện thoại: 092.484.9483
Zalo: 092.484.9483
Facebook: https://facebook.com/giatlathuhuongcom/
Website: Trumsiquangchau.com
Địa chỉ: Ngõ 346 Nam Dư, Trần Phú, Hoàng Mai, Hà Nội.