In Python Class how to create an instance variable, so each instance will have it's own copy of the values
Python: 1) Instance Variable 2) Class Variable
Creating Instance Variables:
In Python, to create an instance variable (i.e., a variable that is specific to each instance of a class), you define the variable inside the __init__ method. This ensures that each instance of the class will have its own copy of the variable.
Here’s an example:
class MyClass:
def __init__(self, value):
# This is an instance variable. Each instance gets its own copy.
self.instance_variable = value
# Creating instances
obj1 = MyClass(10)
obj2 = MyClass(20)
# Accessing instance variables
print(obj1.instance_variable) # Output: 10
print(obj2.instance_variable) # Output: 20
Key Points:
self: Refers to the instance of the class. You useselfto define variables that should be unique to each instance.- Instance variables are defined inside the
__init__method. This ensures that every time an object is created, it has its own independent variable. obj1.instance_variableandobj2.instance_variableare independent because they are tied to different instances (obj1andobj2).
Each object created from the class gets its own instance of instance_variable, and changes to one object’s variable won't affect the others.
In Python, if you want to create variables that are not instance variables but are still part of the class, you can use class variables. These variables are shared across all instances of the class.
Creating Class Variables:
Class variables are defined outside of the __init__ method but inside the class body. They are shared by all instances of the class.
Here’s an example:
class MyClass:
# This is a class variable
class_variable = 0
def __init__(self, value):
# This is an instance variable
self.instance_variable = value
# Creating instances
obj1 = MyClass(10)
obj2 = MyClass(20)
# Accessing class variable through the class itself
print(MyClass.class_variable) # Output: 0
# Accessing class variable through an instance
print(obj1.class_variable) # Output: 0
print(obj2.class_variable) # Output: 0
# Changing class variable
MyClass.class_variable = 100
print(obj1.class_variable) # Output: 100
print(obj2.class_variable) # Output: 100
Key Points:
- Class variables are defined directly within the class, outside of any methods (including
__init__). - They are shared across all instances of the class. If you change the value of a class variable, it changes for all instances.
- Class variables can be accessed either by the class name (
MyClass.class_variable) or through an instance (obj1.class_variable), but they are the same for all instances unless you specifically modify them at the instance level.
Example of Changing a Class Variable at the Instance Level:
# Creating an instance
obj1 = MyClass(10)
# Changing the class variable at the instance level
obj1.class_variable = 200
# Accessing class variable through the instance after change
print(obj1.class_variable) # Output: 200
# Accessing class variable through the class name
print(MyClass.class_variable) # Output: 100 (not affected by the instance-level change)
In this case, obj1.class_variable = 200 doesn't affect the class-level variable but creates an instance-specific variable for obj1 that shadows the class variable.
Comments
Post a Comment