Understanding class and instance variables in Python 3 is one of the most important concepts in object-oriented programming at Progressive Robot — class variables are shared across all objects of a class (like global constants or counters), while instance variables are unique to each object (like name, age, balance), allowing you to model real-world entities with both shared and individual data. Mastering class and instance variables in Python 3 is essential for writing clean, maintainable, scalable code in automation, web APIs, data processing, game development, AI models, and enterprise systems.
In this comprehensive 2025–2026 Progressive Robot guide, you’ll learn exactly how class and instance variables in Python 3 work: definitions, differences, using __init__ and self, shared vs unique behaviour, modifying variables, best practices, common patterns, and real-world examples. All code is tested on Python 3.10–3.13.
Key Takeaways – Class and Instance Variables in Python 3
- Class variables in Python 3 are defined directly inside the class body (outside methods) — shared by all instances.
- Instance variables in Python 3 are defined inside methods (usually __init__) using self. — unique per object.
- Use class variables for constants, counters, default settings shared across objects.
- Use instance variables for per-object data (name, age, balance, status).
- Accessing: ClassName.var (class) or obj.var (instance) — self.var inside methods.
- Modifying class variables via class name affects all objects; modifying via self. creates instance override.
- Follow DRY principle — reduce repetition with proper class/instance separation.
Prerequisites
- Python 3.8+ installed (Progressive Robot recommends 3.12+)
- Basic Python knowledge (classes, objects, __init__, self)
- Interactive shell (python3) or script file
1. Defining Class Variables in Python 3
class Shark:
# Class variables – shared by all Shark objects
animal_type = "fish"
location = "ocean"
followers = 0 # can be used as a counter
# Access via class name
print(Shark.animal_type) # fish
print(Shark.location) # ocean
These variables belong to the class itself — every object shares the same values unless overridden.
2. Defining Instance Variables in Python 3 (via init)
class Shark:
# Class variables
animal_type = "fish"
def __init__(self, name: str, age: int):
# Instance variables – unique per shark object
self.name = name
self.age = age
self.followers = 0 # instance-specific counter
# Create objects
sammy = Shark("Sammy", 5)
stevie = Shark("Stevie", 8)
print(sammy.name) # Sammy
print(stevie.name) # Stevie
print(sammy.age) # 5
print(stevie.age) # 8
Each object (sammy, stevie) has its own name, age, and followers — this is how instance variables in Python 3 work.
3. Accessing & Modifying Class vs Instance Variables
class Shark:
species = "shark" # class variable
def __init__(self, name: str):
self.name = name # instance variable
self.followers = 0
sammy = Shark("Sammy")
stevie = Shark("Stevie")
# Access class variable
print(sammy.species) # shark
print(stevie.species) # shark
# Modify class variable via class name → affects all objects
Shark.species = "great white shark"
print(sammy.species) # great white shark
print(stevie.species) # great white shark
# Modify instance variable → only affects that object
sammy.followers = 100
print(sammy.followers) # 100
print(stevie.followers) # 0 (still original)
4. Full Example – Class & Instance Variables Together
class Shark:
"""Progressive Robot example showing class and instance variables in Python 3."""
# Class variables (shared)
animal_type = "fish"
location = "ocean"
total_sharks = 0 # class-level counter
def __init__(self, name: str, age: int, color: str = "grey"):
# Instance variables (unique)
self.name = name
self.age = age
self.color = color
self.followers = 0
# Update class variable counter
Shark.total_sharks += 1
def swim(self) -> None:
print(f"{self.name} ({self.color}) is swimming in the {self.location}.")
def add_follower(self) -> None:
self.followers += 1
print(f"{self.name} now has {self.followers} followers.")
def main():
sammy = Shark("Sammy", 5, "blue")
stevie = Shark("Stevie", 3)
sammy.swim() # Sammy (blue) is swimming in the ocean.
stevie.swim() # Stevie (grey) is swimming in the ocean.
sammy.add_follower() # Sammy now has 1 followers.
sammy.add_follower() # Sammy now has 2 followers.
print(f"Total sharks created: {Shark.total_sharks}") # 2
if __name__ == "__main__":
main()
5. Best Practices & Modern Tips (2025–2026)
- Use class variables for shared constants (PI = 3.14159, DEFAULT_COLOR = “grey”) or counters.
- Use instance variables for per-object data (self.name, self.balance).
- Always define instance variables inside __init__ using self..
- Add docstrings and type hints: class Shark: “””…””” def __init__(self, name: str) -> None:
- Avoid modifying class variables via self. unless intentional override.
- Use @classmethod or @staticmethod when methods don’t need instance data.
- Keep __init__ simple — move complex setup to other methods.
How Class and Instance Variables Work in Python 3 – FAQ (2025–2026)
- What are class variables in Python 3?
Defined directly in class body — shared by all objects when using class and instance variables in Python 3. - What are instance variables in Python 3?
Defined with self. inside __init__ — unique per object. - How do I access class variables in Python 3?
Via class name (Shark.species) or instance (obj.species) — both work. - What happens if I change a class variable via self in Python 3?
Creates an instance override — original class variable unchanged for other objects. - When should I use class vs instance variables in Python 3?
Class → shared data/constants; Instance → unique per-object data.
Summary
You now know exactly how class and instance variables in Python 3 work: definitions, differences, __init__ & self, shared vs unique behaviour, modifying variables, full examples, and Progressive Robot best practices.
Mastering class and instance variables in Python 3 unlocks proper OOP design — shared constants, per-object state, scalable models — essential for building clean, maintainable applications at Progressive Robot.
Recommended Next Tutorials
- Python Inheritance & Polymorphism (OOP Deep Dive)
- Python Properties, Getters & Setters
- Python Magic Methods (str, repr, etc.)
- Build a Bank Account / User Profile System with Classes
- Python OOP Cheat Sheet