Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, there are two main types of variables that can be associated with a class: class variables and instance variables. In this tutorial, we will cover the differences between class variables and instance variables and demonstrate how to use them.
Class Variables
Class variables are variables that are shared by all instances of a class. They are defined within a class but outside any instance methods. Class variables are typically used for data that is common to all instances of a class.
Instance Variables
Instance variables are variables that belong to each individual instance of a class. They are defined within instance methods using the self
keyword. Instance variables are used for data that is specific to each instance of a class.
Example: Using Class Variables and Instance Variables
Let's create a class called Dog
with both class variables and instance variables:
class Dog: # Class variable species = "Canis lupus familiaris" def __init__(self, name, age): # Instance variables self.name = name self.age = age def bark(self): print(f"{self.name} says woof!")
In this example, species
is a class variable shared by all instances of the Dog
class. The name
and age
variables are instance variables that are specific to each Dog
instance.
Now, let's create some instances of the Dog
class and observe the behavior of class variables and instance variables:
# Creating Dog instances dog1 = Dog("Buddy", 3) dog2 = Dog("Daisy", 2) # Accessing instance variables print(dog1.name) # Output: Buddy print(dog2.name) # Output: Daisy # Accessing the class variable print(dog1.species) # Output: Canis lupus familiaris print(dog2.species) # Output: Canis lupus familiaris # Modifying a class variable Dog.species = "Canis lupus" print(dog1.species) # Output: Canis lupus print(dog2.species) # Output: Canis lupus
In this example, you can see that the instance variables name
and age
are unique to each Dog
instance, while the class variable species
is shared by all instances of the class. When we modify the class variable species
, the change is reflected in all instances of the Dog
class.
In conclusion, class variables are shared among all instances of a class and are used for data that is common to all instances, while instance variables are specific to each instance and are used for data that is unique to each instance. Understanding the differences between class variables and instance variables is essential for designing and implementing classes in Python effectively.
Defining class variables and instance variables in Python:
self
.class MyClass: class_variable = "I am a class variable" def __init__(self, instance_variable): self.instance_variable = instance_variable
Accessing and modifying class attributes in Python:
MyClass.class_variable = "Modified class variable"
Initializing instance variables in Python classes:
__init__
method using self
.def __init__(self, instance_variable): self.instance_variable = instance_variable
Inheritance and variable scope in Python classes:
class ChildClass(MyClass): def __init__(self, instance_variable, child_variable): super().__init__(instance_variable) self.child_variable = child_variable
Encapsulation with private class and instance variables in Python:
class MyClass: _protected_variable = "I am a protected variable" __private_variable = "I am a private variable"