Below is a clear, complete, and exam-oriented explanation of Accessing Attributes, Editing Class Attributes, and Built-in Class Attributes in Python OOP — perfect for BCA/MCA/B.Tech students.
⭐ 1. Accessing Attributes in Python
Attributes are variables that belong to a class or to an object.
There are two types:
- Instance attributes → belong to each object
- Class attributes → shared among all objects
To access attributes, we use the dot operator (.).
✔ Accessing Instance Attributes
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
s1 = Student("Amit", 101)
print(s1.name) # Access attribute
print(s1.roll)
✔ Accessing Class Attributes
class Student:
college = "XYZ University" # class attribute
s1 = Student("Amit", 101)
print(s1.college) # via object
print(Student.college) # via class
✔ Both ways are allowed
❗ Best practice → access class attributes using class name
⭐ 2. Editing Class Attributes
Class attributes can be modified using:
✔ A. Class name (Recommended)
class Student:
college = "XYZ University"
Student.college = "ABC College" # modify class attribute
s1 = Student("Amit", 101)
s2 = Student("Rita", 102)
print(s1.college) # ABC College
print(s2.college) # ABC College
👉 All objects see the updated value because class attributes are shared.
✔ B. Editing via object (Not recommended)
This does not change the class attribute.
It creates a new instance attribute.
s1.college = "New College"
print(s1.college) # New College
print(Student.college) # ABC College (unchanged)
✔ Only s1 is affected.
✔ Confirms class attribute is different from instance attribute.
⭐ 3. Editing Instance Attributes
Instance attributes are edited per object.
s1.name = "Arjun"
print(s1.name)
✔ Only that object gets updated.
⭐ 4. How Python Looks for an Attribute (Resolution Order)
When accessing s1.college:
- Python checks instance attributes
- If not found → checks class attributes
- If still not found → checks parent classes (inheritance)
This is known as Attribute Resolution Order (MRO).
⭐ 5. Built-in Class Attributes
Python classes come with several built-in attributes that give metadata about the class.
Here are the most important ones for exams:
✔ 1. __dict__
Stores all attributes of a class or object as a dictionary.
class Student:
college = "XYZ University"
print(Student.__dict__)
For an object:
s1 = Student()
print(s1.__dict__)
✔ 2. __doc__
Shows the documentation string of the class.
class Student:
"""This class represents student information."""
pass
print(Student.__doc__)
✔ 3. __name__
Holds the class name.
print(Student.__name__)
✔ 4. __module__
Shows the module where the class is defined.
print(Student.__module__)
If in the same file → output is "__main__".
✔ 5. __bases__
Shows base classes (important for inheritance).
class A:
pass
class B(A):
pass
print(B.__bases__) # (<class '__main__.A'>,)
✔ 6. __class__
Used with objects to show which class they belong to.
s1 = Student()
print(s1.__class__)
⭐ Example Demonstrating All Concepts
class Employee:
"""Employee class"""
company = "TechCorp" # class attribute
def __init__(self, name, salary):
self.name = name # instance attribute
self.salary = salary
# Creating objects
e1 = Employee("Ravi", 50000)
# Accessing attributes
print(e1.name)
print(Employee.company)
# Editing class attribute
Employee.company = "NewCorp"
# Built-in class attributes
print(Employee.__name__)
print(Employee.__doc__)
print(Employee.__dict__)
print(Employee.__module__)
print(Employee.__bases__)
⭐ Exam-Ready Summary
Accessing Attributes:
- Use the dot operator (
object.attributeorclass.attribute). - Instance attributes differ for each object, class attributes are shared.
Editing Class Attributes:
- Use
ClassName.attribute = valueto update for all objects. - Editing via object creates a new instance attribute and does not change the class attribute.
Built-in Class Attributes:
__dict__→ dictionary of class attributes__doc__→ class documentation__name__→ class name__module__→ module name__bases__→ base classes
