Classes and Objects
A class defines a new type; an object is one real instance of that type, the same way an integer value like 5 is one instance of the built-in int type.
What You Already Know About Types and Values
You have been working with types and values since you started programming. When you write the number 5, you are creating a value of type int. When you write the text 'hello', you are creating a value of type str. Each of these values is a real, concrete thing that exists in your program. The type int is like a blueprint or template that describes what an integer is and what you can do with it. The value 5 is one specific instance of that blueprint. This relationship between a blueprint and the things made from it is exactly what classes and objects are.
A class is a type definition—a template. An object is one real instance of that type, just like 5 is one instance of int and 'hello' is one instance of str.
The Class-as-Blueprint Model
A class is a definition you write that describes what kind of data and behaviors things of that type should have. It is not itself a real thing—it is a plan. When you create an object from a class, you are making a real instance of that plan. That object is the actual thing that exists in your program and holds real data. Think of a class like an architectural blueprint for a house. The blueprint is not a house; it is a description of what a house should look like and what rooms it should have. Each house built from that blueprint is a real object—a place where people actually live. The blueprint can be used to build many houses, and each house will have the same structure but different furniture, different colors, and different people living in it.
Consider a Dog class. The class defines that a dog has a name, an age, and a breed. It also defines that a dog can bark and eat. These are the rules and structure. But the Dog class itself is not a dog—it is the idea of what a dog is. When you create an object from the Dog class, you get a real dog: maybe a dog named Buddy who is 3 years old and is a Golden Retriever. You can create another object from the same Dog class: a dog named Max who is 5 years old and is a Labrador. Both are dogs (both follow the Dog class structure), but they are different objects with different data.
Built-in Types Are Already Classes
Python's built-in types like str, int, and list are not special magic—they are classes, just like any class you might write yourself. Every string you use in a program is a real object of the str class. Every list is a real object of the list class. This means that the relationship between the str class and the string 'hello' is exactly the same as the relationship between a Dog class you write and a Dog object you create. The str class defines what strings can do: they can be indexed, they can be concatenated, they have methods like upper() and lower(). Each time you write 'hello', you are creating one real object of the str class with specific data (the characters h-e-l-l-o).
Built-in Python values are real objects of built-in classes. A Python list is a real, concrete example of an object built from a class—the same relationship applies to any user-defined class and its instances.
Fields and Methods: What Objects Hold
A class can define fields—variables that belong to that class specifically—which are only usable through an actual object (instance) of that class. A class can also define methods—functions that belong to that class and can be called on objects. Fields hold the data that makes each object unique. Methods define the behaviors that objects of that class can perform. When you create an object, it gets its own copy of the fields defined by its class. Two objects from the same class will have the same field names and the same methods, but the values stored in those fields will be different for each object.
A Dog class might define three fields: name, age, and breed. It might also define two methods: bark and eat. When you create a Dog object named buddy, that object has its own name field (set to 'Buddy'), its own age field (set to 3), and its own breed field (set to 'Golden Retriever'). The buddy object can call the bark() method and the eat() method. If you create another Dog object named max, it will also have name, age, and breed fields, and it will also be able to call bark() and eat(). But max's fields will have different values: maybe name is 'Max', age is 5, and breed is 'Labrador'. Both objects share the same structure and the same methods, but they have different data.
Multiple Objects, Same Class, Different State
Let's trace through what happens when you create two objects from the same class. Imagine you have a Dog class with fields for name, age, and breed. You create the first object: buddy = Dog('Buddy', 3, 'Golden Retriever'). Now buddy is a real object with its own data. You create a second object: max = Dog('Max', 5, 'Labrador'). Now max is a different real object with its own data. Both buddy and max follow the Dog class structure, but they are completely separate objects. Changing buddy's age does not change max's age. They share the same blueprint (the Dog class), but they are independent instances with independent state.
Two objects from the same class share the same structure and methods, but each object has its own independent state (field values). Changing one object does not affect the other.
Common Mistakes and Misconceptions
Confusing the class with the object
The class is the blueprint; the object is the real thing made from the blueprint. They are different kinds of entities. You cannot store data in a class the way you can in an object.
Fix:
Remember: Dog is the class (the template). buddy is an object (a real instance). You define the class once, then create many objects from it.Thinking that built-in types like str and int are not classes
Built-in types are classes just like any class you define. The str class defines what strings are and what they can do. Every string is a real object of the str class.
Fix:
Recognize that str, int, list, and other built-in types are classes. The value 'hello' is an object of the str class, just as buddy would be an object of the Dog class.Thinking that all objects of the same class must have identical data
Objects share the same structure and methods, but each object stores its own data. The class defines what fields exist, not what values they hold.
Fix:
Each object has its own independent state. Two Dog objects will both have name, age, and breed fields, but the values in those fields are separate for each object.
Why This Matters
Understanding the difference between a class and an object is fundamental to object-oriented programming. It lets you write code that is organized, reusable, and clear. When you define a class, you are creating a reusable template. You can create as many objects from that template as you need, and each one will be independent. This is much more powerful than writing separate code for each individual thing. It also helps you understand how Python's built-in types work. When you call a method on a string like 'hello'.upper(), you are calling a method on a real object of the str class. Understanding that str is a class just like any class you write yourself makes the whole language more coherent.
Practice: Recognizing Classes and Objects
For each of the following, identify whether you are looking at a class or an object. Explain your reasoning. 1) The str type. 2) The string 'hello'. 3) A Car class you define with fields for color, make, and model. 4) A Car object you create with color='blue', make='Toyota', model='Camry'. 5) The int type. 6) The number 42.
Hints
- A class is a template or definition. An object is a real instance of that class.
- Built-in types like str and int are classes.
- Every value you create in Python is an object of some class.
Imagine you define a Book class with fields for title, author, and pages. You create two objects: book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', 180) and book2 = Book('1984', 'George Orwell', 328). Explain what is the same between book1 and book2, and what is different. What would happen if you changed book1's title to 'The Beautiful and Damned'? Would that affect book2?
Hints
- Think about what the Book class defines and what each object stores.
- Each object has its own independent state.
- Changing one object does not affect another object.
Summary
A class is a type definition—a blueprint that describes what kind of data and behaviors things of that type should have. An object is one real instance of that type. This is the same relationship that exists between the int type and the value 5, or between the str type and the string 'hello'. Built-in Python types like str, int, and list are classes, and every value you use is a real object of one of those classes. A class can define fields (variables that belong to objects of that class) and methods (functions that objects can call). Multiple objects created from the same class share the same structure and methods, but each object has its own independent state—its own field values. Understanding this distinction is key to writing organized, reusable, and clear code.
Key Takeaways
- A class is a template or blueprint; an object is a real instance of that class.
- Built-in types like str and int are classes, and every value in Python is an object of some class.
- A class defines fields (data) and methods (behaviors) that objects of that class will have.
- Multiple objects from the same class share structure but have independent state—changing one object does not affect another.
- Understanding classes and objects is fundamental to writing organized, reusable code.