Python Class Attributes: Understanding Instance and Class Variables

Learn about Python class attributes, which are variables defined in a class and shared among all its instances. Discover the difference between instance attributes and class attributes, and how to access them using the class name or object.



Python - Class Attributes

The properties or variables defined inside a class are called Attributes. They provide information about the type of data a class contains. There are two types of attributes in Python: instance attributes and class attributes.

Class Attributes (Variables)

Class attributes are variables that belong to a class and are shared among all instances of that class. They are defined in the class but outside any method. They can be accessed by the name of the class or the object.

Accessing Class Attributes

The object name followed by dot notation (.) is used to access class attributes.

Example

class Employee:
   name = "John Doe"
   age = "35"

# instance of the class
emp = Employee()
# accessing class attributes
print("Name of the Employee:", emp.name)
print("Age of the Employee:", emp.age)
        
Output

Name of the Employee: John Doe
Age of the Employee: 35
        

Modifying Class Attributes

To modify the value of a class attribute, assign a new value using the class name followed by dot notation and the attribute name.

Example

class Employee:
   # class attribute    
   empCount = 0
   def __init__(self, name, age):
      self.__name = name
      self.__age = age
      # modifying class attribute
      Employee.empCount += 1
      print ("Name:", self.__name, ", Age: ", self.__age)
      # accessing class attribute
      print ("Employee Count:", Employee.empCount)

e1 = Employee("Aditi", 28)
print()
e2 = Employee("Vikram", 32)
        
Output

Name: Aditi , Age:  28
Employee Count: 1

Name: Vikram , Age:  32
Employee Count: 2
        

Significance of Class Attributes

Class attributes are important because:

  • They define properties that have the same value for every object of the class.
  • They set default values for objects.
  • They help in creating singletons, which are instantiated only once and used in different parts of the code.

Built-In Class Attributes

Every Python class has the following built-in attributes, which can be accessed using the dot operator:

  • __dict__ - Dictionary containing the class's namespace.
  • __doc__ - Class documentation string or None if undefined.
  • __name__ - Class name.
  • __module__ - Module name where the class is defined.
  • __bases__ - Tuple containing the base classes.

Access Built-In Class Attributes

To access built-in class attributes, use the class name followed by a dot (.) and the attribute name.

Example

class Employee:
   def __init__(self, name="Aditi", age=28):
      self.name = name
      self.age = age
   def displayEmployee(self):
      print ("Name : ", self.name, ", age: ", self.age)
print ("Employee.__doc__:", Employee.__doc__)
print ("Employee.__name__:", Employee.__name__)
print ("Employee.__module__:", Employee.__module__)
print ("Employee.__bases__:", Employee.__bases__)
print ("Employee.__dict__:", Employee.__dict__)
        
Output

Employee.__doc__: None
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: (,)
Employee.__dict__: {'__module__': '__main__', '__init__': , 'displayEmployee': , '__dict__': , '__weakref__': , '__doc__': None}
        

Instance Attributes

An instance attribute in Python is a variable specific to an individual object of a class. It is defined inside the __init__() method using the self parameter.

Example

class Student:
   def __init__(self, name, grade):
      self.__name = name
      self.__grade = grade
      print ("Name:", self.__name, ", Grade:", self.__grade)

# Creating instances 
student1 = Student("Kiran", "A")
student2 = Student("Lina", "B")
        
Output

Name: Kiran , Grade: A
Name: Lina , Grade: B
        

Instance Attributes Vs Class Attributes

SNo. Instance Attribute Class Attribute
1 Defined inside the __init__() function. Defined inside the class but outside the __init__() function.
2 Accessed using the object name followed by dot notation. Accessed by both class name and object name.
3 Value is not shared among other objects. Value is shared among other objects of the class.
4 Changes affect only the object within which it is defined. Changes affect all objects of the given class.