WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Basic · question 18 of 100

What are the differences between a class and an object in Python?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

In Python, a class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines a set of attributes and methods that are shared by all instances of the class, while an object is a specific instance of the class that has its own unique set of attribute values.

Here is an example of a class definition in Python:

python Copy code class Person: def __init__(self, name, age): self.name = name self.age = age

def get_name(self): return self.name

def get_age(self): return self.age

In this example, we define a class called Person that has two attributes name and age, and two methods get_name and get_age. The __init__ method is a special method that is called when a new object is created, and is used to initialize the object’s attributes.

To create an object of the Person class, we would call the class and pass in the required arguments, like this:

python Copy code person1 = Person(’Alice’, 30)

In this example, we create an object called person1 that is an instance of the Person class. We pass in the arguments ’Alice’ and 30 to the class, which are used to initialize the object’s name and age attributes.

We can access the object’s attributes and methods using the dot notation, like this:

python Copy code print(person1.name) # Output: ’Alice’ print(person1.get_age()) # Output: 30

In summary, a class is a template for creating objects, while an object is an instance of a class. A class defines a set of attributes and methods that are shared by all instances of the class, while an object is a specific instance of the class that has its own unique set of attribute values. Classes are useful for organizing code and creating reusable objects, while objects are useful for representing specific instances of a class in a program.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics