StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Types of Variables
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP
Classes : Namespaces
Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP

In Python, within the context of Object-Oriented Programming and classes, there are two main types of variables: instance variables and class variables. They differ in how they store data and how that data is shared among objects.

1. Instance Variables

Instance variables are unique to each individual instance (or object) of a class. Every time you create a new object, it gets its own separate copy of the instance variables. Changes to an instance variable in one object do not affect any other object.

  • Use Case: To store data that is specific to a particular object. For example, in a Car class, the color, model, and license_plate would be instance variables because each car has its own unique values for these attributes.

  • How they are defined: They are always defined inside the __init__ constructor method, using the self keyword (e.g., self.name = name).

Python

# --- Instance Variables Example ---

 

class Car:

    def __init__(self, color, model):

        # 'color' and 'model' are instance variables.

        # Each Car object will have its own values for these.

        self.color = color

        self.model = model

 

# Create two different instances of the Car class.

car1 = Car("Red", "Sedan")

car2 = Car("Blue", "SUV")

 

# Each object has its own unique data.

print(f"Car 1 is a {car1.color} {car1.model}.")

print(f"Car 2 is a {car2.color} {car2.model}.")

 

# Changing an instance variable for car1 does not affect car2.

car1.color = "Black"

print(f"\nAfter repainting, Car 1 is now a {car1.color} {car1.model}.")

print(f"Car 2 is still a {car2.color} {car2.model}.")


2. Class Variables

Class variables are shared by all instances of a class. They belong to the class itself, not to any specific object. If you change a class variable, the change will be reflected in all instances of that class.

  • Use Case: To store data that is constant or common for all objects created from the class. For example, in a Car class, the number_of_wheels would be a good class variable because all cars have four wheels.

  • How they are defined: They are defined directly inside the class, but outside of any methods (usually at the top).

Python

# --- Class Variables Example ---

 

class Car:

    # 'number_of_wheels' is a class variable.

    # It is shared by all instances of the Car class.

    number_of_wheels = 4

 

    def __init__(self, color, model):

        # These are still instance variables.

        self.color = color

        self.model = model

 

car1 = Car("Red", "Sedan")

car2 = Car("Blue", "SUV")

 

# Both instances access the SAME class variable.

print(f"Car 1 has {car1.number_of_wheels} wheels.")

print(f"Car 2 has {car2.number_of_wheels} wheels.")

 

# If we change the class variable, it affects all instances.

Car.number_of_wheels = 3 # Let's imagine a factory recall

print("\nAfter a factory recall...")

print(f"Car 1 now has {car1.number_of_wheels} wheels.")

print(f"Car 2 now has {car2.number_of_wheels} wheels.")

 

Classes Namespaces
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website