Classes and Objects

Python Classes and Objects: Creating Your First Object

Suppose we are developing a program to manage student details. We can begin by storing a student’s name, roll number, and marks in separate variables.

name = "Rahul"
roll_number = 101
marks = 85

print(name)
print(roll_number)
print(marks)

Output:

Rahul
101
85

These variables represent the details of one student. Now, suppose we want to store another student’s details.

name1 = "Rahul"
roll_number1 = 101
marks1 = 85

name2 = "Anjali"
roll_number2 = 102
marks2 = 92

This works, but the relationship between each student and their details depends on how we name the variables.

Python allows us to represent each student as an object. Instead of keeping the details in separate variables, we can store them as attributes belonging to that student object.

Defining a Student Class

Before creating our student objects, we will define a class.

class Student:
    pass

The class keyword introduces a class definition. Here, Student is the class name.

The indented pass statement is a placeholder. It does nothing, but it allows us to leave the class body empty for now.

By convention, Python class names use CapWords, where each word starts with a capital letter:

Student
BankAccount
ShoppingCart

A class defines a type of object. Our Student class provides the starting point for creating individual student objects. Later, we will add code inside the class to initialize their data and define their actions.

Creating the First Object

After defining the class, we can create an object by calling the class name with parentheses.

class Student:
    pass


student1 = Student()

In this statement:

student1 = Student()

Student() creates a new instance of the Student class, and the variable student1 refers to that object.

An instance is an object created from a particular class. Therefore, we can describe student1 as referring to either a Student object or an instance of Student.

Notice the difference:

Student       # Refers to the class
Student()     # Creates an instance of the class

The parentheses are needed here to create the object.

Giving the Object Some Data

Our student object does not yet have a name, roll number, or marks. We can add these details using dot notation.

student1.name = "Rahul"
student1.roll_number = 101
student1.marks = 85

The general form is:

object_name.attribute_name = value

An attribute is a named piece of data associated with an object.

For example:

student1.name = "Rahul"

This statement assigns "Rahul" to the name attribute of the object referenced by student1.

Our student object now contains these attributes:

Attribute Value
name "Rahul"
roll_number 101
marks 85

In this simple class, Python allows us to add attributes after creating the object. In the next tutorial, we will arrange for these attributes to be initialized when we create each student.

Reading the Student’s Details

We use the same dot notation to access an attribute.

print(student1.name)
print(student1.roll_number)
print(student1.marks)

Output:

Rahul
101
85

We can include labels to make the output easier to read.

print("Name:", student1.name)
print("Roll Number:", student1.roll_number)
print("Marks:", student1.marks)

Output:

Name: Rahul
Roll Number: 101
Marks: 85

Here is the complete program so far:

class Student:
    pass


student1 = Student()

student1.name = "Rahul"
student1.roll_number = 101
student1.marks = 85

print("Name:", student1.name)
print("Roll Number:", student1.roll_number)
print("Marks:", student1.marks)

Follow the program from top to bottom: we define the class, create an object, assign its attributes, and then read those attributes.

Updating an Attribute

Suppose Rahul’s marks change from 85 to 89 after a correction. We can assign a new value to the existing attribute.

student1.marks = 89

print("Updated Marks:", student1.marks)

Output:

Updated Marks: 89

We can also use the current value in a calculation. To add two marks:

student1.marks = student1.marks + 2

print("Marks After Adding Two:", student1.marks)

Output:

Marks After Adding Two: 91

The expression on the right reads the current marks, adds 2, and assigns the result back to the attribute.

The shorter form is:

student1.marks += 2

These are alternative ways to perform the same update. Use only one of them when adding two marks.

Creating Another Student

We can use the same class to create a second object.

student2 = Student()

student2.name = "Anjali"
student2.roll_number = 102
student2.marks = 92

We now have two separate student objects.

Attribute student1 student2
name "Rahul" "Anjali"
roll_number 101 102
marks 91 92

Both objects come from the Student class, but each holds its own data.

print(student1.name)
print(student2.name)

Output:

Rahul
Anjali

Updating one student’s marks does not change the other student’s marks.

student1.marks = 95

print("Rahul's Marks:", student1.marks)
print("Anjali's Marks:", student2.marks)

Output:

Rahul's Marks: 95
Anjali's Marks: 92

The attribute name marks is the same, but the object before the dot determines which student’s marks we access.

Putting It Together

Run this example as a complete program:

class Student:
    pass


# Create the first student
student1 = Student()

student1.name = "Rahul"
student1.roll_number = 101
student1.marks = 85

# Create the second student
student2 = Student()

student2.name = "Anjali"
student2.roll_number = 102
student2.marks = 92

# Update the first student's marks
student1.marks = 89

# Display the first student's details
print("Student 1")
print("Name:", student1.name)
print("Roll Number:", student1.roll_number)
print("Marks:", student1.marks)

print()

# Display the second student's details
print("Student 2")
print("Name:", student2.name)
print("Roll Number:", student2.roll_number)
print("Marks:", student2.marks)

Output:

Student 1
Name: Rahul
Roll Number: 101
Marks: 89

Student 2
Name: Anjali
Roll Number: 102
Marks: 92

We defined the class only once and used it to create two objects. Each call to Student() creates a new instance in this example.

Another Name for the Same Object

Consider a slightly different statement:

another_student = student1

This does not create another Student object. It makes another_student refer to the same object as student1.

another_student.marks = 98

print(student1.marks)

Output:

98

The change is visible through student1 because both variables refer to the same object.

Compare the two operations:

Statement Result
student2 = Student() Creates a new Student object
another_student = student1 Adds another reference to the existing object

We can check whether two variables refer to the same object using is.

print(student1 is another_student)
print(student1 is student2)

Output:

True
False

Accessing an Attribute Before Assigning It

In our current class, creating an object does not automatically give it student details.

student3 = Student()

print(student3.name)

This raises an error:

AttributeError: 'Student' object has no attribute 'name'

Although other student objects have a name attribute, we have not assigned one to student3.

Assign it before trying to read it:

student3.name = "Kiran"

print(student3.name)

Output:

Kiran

Attribute names are also case-sensitive. name and Name are different names.

print(student3.name)   # Works
print(student3.Name)   # Raises AttributeError

Use consistent spelling when assigning and accessing attributes.

Our program currently repeats three assignments every time we create a student:

student1 = Student()
student1.name = "Rahul"
student1.roll_number = 101
student1.marks = 85

It is also possible to forget an attribute, as we saw with student3.name.

In the next lesson, we will introduce __init__() so that we can provide these details when creating the object:

student1 = Student("Rahul", 101, 85)

This form requires an updated class definition. We will build that definition step by step, introduce self, and add methods that display and update the student’s details.

Next Topic Initializing Objects and Adding Methods