Skip to main content
Home/Blog/Python/Python Objects and Classes Guide | OOP Fundamentals
Python

Python Objects and Classes Guide | OOP Fundamentals

Master object-oriented programming in Python with practical examples and best practices for efficient development

Python Objects and Classes Guide | OOP Fundamentals

Today we will discuss what objects are, how objects relate to classes, and when you should use objects and classes in your Python applications. This guide provides practical examples to help you master these essential programming concepts.

In Python, a class is a blueprint and an object is a thing built from that blueprint. You define a class once with the class keyword to describe what data an entity holds (its attributes) and what it can do (its methods). You then create as many objects (also called instances) from that class as you need, each with its own independent data but sharing the same methods. The __init__ method runs automatically on creation to set up each object's starting attributes, and self inside every method refers to the specific object being acted on.

That's the one-paragraph summary an AI gives you. What it can't show you is the shape of the relationship: how one class fans out into many independent objects, where self actually points, and which of the many "attribute" concepts you should reach for. The animated diagram, the class-vs-object table, and the class-attribute gotcha below make those concrete.

One Class, Many Objects (at a Glance)

The single most important mental model in object-oriented programming is that one class produces many independent objects. The class is written once; each object carries its own copy of the instance attributes.

One Dog class producing three independent Dog objects A single class blueprint on the left connects by arrows to three separate object instances on the right, each holding its own name and hair length while sharing the same methods. CLASS (blueprint) class Dog: def __init__(self, name) self.name = name self.hairlength = 10 def cut_hair(self, n) Dog(...) max = Dog("Max") name = "Max" hairlength = 8 bella = Dog("Bella") name = "Bella" hairlength = 10 rex = Dog("Rex") name = "Rex" hairlength = 3

Notice that all three objects share the same cut_hair method (defined once in the class) but each holds its own hairlength. Cutting Max's hair leaves Bella and Rex untouched. That independence is the whole point.

Class vs. Object: The Distinction That Trips People Up

Beginners constantly blur these two words. Here is the precise difference:

ClassObject (Instance)
What it isA blueprint / templateA concrete thing built from the blueprint
How manyWritten onceCreated as many times as you need
Exampleclass Dog:mydog = Dog("Max")
HoldsMethod definitions + class attributesIts own instance attribute values
LivesIn your source codeIn memory at runtime
AnalogyThe architectural drawingAn actual house built from the drawing
When you use itDefine the shape and behavior onceEvery time you need a new, independent entity

If you can answer "am I describing the shape, or am I making one of the things?" you can always tell which you need.

What are Objects?

Objects are containers that hold a collection of attributes and functions. Think of objects as real-world entities that have properties (attributes) and can perform actions (methods). For example, you might create an application that tracks dogs.

Object Attributes (Properties)

For each dog you are tracking, you might create an object. Each dog object would have a collection of attributes like:

  • Color: The dog's fur color
  • Age: How old the dog is
  • Breed: What type of dog it is

Object Methods (Actions)

Each dog object also has actions associated with it. For example, you might:

  • Take the dog for a walk
  • Cut the dog's hair
  • Give the dog a bath

Working with Objects – Example

Let's assume you have imported a library that gives you access to an object called Dog. Here's how you would work with it:

# Create a new dog object named Max
mydog = Dog("Max")

# Set Max's breed to Chihuahua
mydog.breed = "Chihuahua"

# Shorten Max's hair length
mydog.cut_hair(2)

# Look at all of the attributes
print(mydog.name)
print(mydog.breed)
print(mydog.hairlength)

What are Classes?

As we discussed in the previous section, an object is a container that holds various attributes and functions. A class is the code that you use to create an object. Think of a class as a blueprint or template for creating objects.

Naming convention: Python's official style guide (PEP 8) recommends CapWords (also called PascalCase) for class names, so Dog, BankAccount, HttpClient. The lowercase dog you may see in older tutorials works, but it makes classes harder to tell apart from variables and functions. We use Dog throughout.

Creating a Basic Class

To create a new class, we use the keyword class. Let's create a new class called Dog with two attributes: breed and name:

class Dog:
    name = ""
    breed = ""

# Create a new instance of this class
mydog = Dog()

# Set the name and breed
mydog.name = "Max"
mydog.breed = "Chihuahua"

# Print the dog's name
print(mydog.name)

Adding an Initialization Function

We probably want to treat the name of the dog as a unique attribute for each of our dogs and ensure that all dogs have names. To do this, we need to add an __init__ function that will be called every time we create a new dog object:

class Dog:
    def __init__(self, name):
        self.name = name
        self.breed = ""

# Create a new dog object named Max
mydog = Dog("Max")

print(mydog.name)  # Output: Max

Key Concept: The self.name assignment tells Python to store the name we passed in on this specific object. self always refers to the particular instance the method is running on, which is how Dog("Max") and Dog("Bella") end up with different names even though they share one class.

What Actually Happens When You Write Dog("Max")

Understanding the order of events removes most of the confusion around __init__ and self. Here is the sequence:

The four steps Python runs when you create an object Left to right: call the class, Python allocates a new empty object, __init__ runs with self bound to that object, and the finished object is returned to your variable. 1. CALL Dog("Max") you call the class 2. ALLOCATE empty object created in memory 3. __init__ self.name = "Max" self = new object 4. RETURN mydog bound to variable

You never call __init__ yourself and you never pass self yourself. Python wires both up for you the moment you call the class.

Adding Methods to Classes

There are various things we will do with our dog. Perhaps we need to cut our dog's hair on occasion. Let's add a new variable called hairlength and a function called cut_hair:

class Dog:
    breed = ""

    # Add hairlength variable to the init function
    def __init__(self, name):
        self.name = name
        self.hairlength = 10

    # Declare the hair-cutting method
    def cut_hair(self, howmuch):
        self.hairlength = self.hairlength - howmuch

# Create a new Dog object named Max
mydog = Dog("Max")

# Call the method to cut the dog's hair
mydog.cut_hair(2)

# Print how long the hair length is now
print(mydog.hairlength)  # Output: 8

As you can see above, we start by creating a new dog object, then we call the cut_hair method. The default value set in __init__ is a hairlength of 10. After cutting 2, the output from the print statement is 8.

The Class-Attribute Gotcha

Notice breed = "" sits directly in the class body, while self.name and self.hairlength are set inside __init__. That difference matters more than it looks:

Instance attributeClass attribute
Where definedself.x = ... inside a methodx = ... in the class body
Shared?Unique to each objectShared by all instances
Good forPer-object state (name, hair length)Shared constants (species, wheel count)
Common bug—A mutable default like tricks = [] is shared, so appending on one dog appends to all

The safe rule: keep per-object data as instance attributes set in __init__, and reserve class attributes for values that genuinely should be the same for every instance. If you have ever been baffled by "why did adding a trick to one dog add it to all of them," a mutable class attribute is almost always the cause. This is closely related to how mutable vs. immutable objects behave in Python generally.

When Should You Use Objects and Classes?

Objects and classes allow you to break up your application into smaller, manageable pieces. These smaller pieces can be independently modified and tested. If you have done things right, you can modify one class without worrying about breaking another class.

But classes are not always the right tool. Python is deliberately multi-paradigm, and forcing a class where a plain function would do just adds ceremony. Use this quick decision guide:

SituationReach forWhy
A short script or one-off transformPlain functionsNo lasting state to model; a class adds noise
Data plus the behavior that operates on it, bound togetherA classEncapsulation keeps related code in one place
Many independent things of the same shapeA classOne blueprint, many instances is exactly the class pattern
Just grouping related constantsA module or dataclassLighter weight than a full behavioral class
Modeling a real-world entity with changing stateA classState (attributes) + actions (methods) map cleanly

Key Benefits of Object-Oriented Programming

  • Modularity: Break complex programs into smaller, manageable pieces
  • Reusability: Create classes once and use them multiple times
  • Maintainability: Easier to modify and debug isolated components
  • Scalability: Better organization for larger teams and bigger projects

Best Practices

As your programs get bigger, and you work on larger teams, organization becomes increasingly important. Generally, the rule is that classes should only do one thing, and do that one thing really well. Think of each class as a mini-program within your main program.

Pro Tip: Single Responsibility Principle

For more advanced concepts, consider learning about the SOLID principles of object-oriented design, which provide guidelines for writing clean, maintainable object-oriented code. The first, the Single Responsibility Principle, is simply the "one class, one job" rule stated formally: a class should have only one reason to change.

Summary

Today we have discussed what objects and classes are in Python. We covered how to use objects, how to create classes, and how the two are inter-related. Understanding these concepts is fundamental to writing effective Python code that can scale as your applications grow.

Key Takeaways

  • Objects are containers that hold attributes and methods
  • Classes are blueprints for creating objects; one class produces many independent objects
  • __init__ runs automatically on creation and sets up each object's starting attributes
  • self refers to the specific object a method is acting on
  • Keep per-object data as instance attributes; reserve class attributes for genuinely shared values
  • Object-oriented programming improves code organization and maintainability, but plain functions are often the better fit for short scripts

Keep Learning

Frequently Asked Questions

Find answers to common questions

Use class when: you have data and operations that belong together (bank account with balance and deposit/withdraw methods), need to maintain state between operations (game character with health/inventory), or creating multiple instances of same type (multiple users, each with their own data). Use functions when: simple operations without state (calculate tax on amount), utility operations (format string, validate email), or one-time tasks. Example needing class: shopping cart (data: items, total; methods: add_item, remove_item, calculate_total—state changes over time). Example using functions: calculate_discount(price, percentage) (no state, just input→output). Don't overcomplicate with classes for simple tasks. Start with functions, refactor to class when you find yourself passing same data to multiple related functions.

Instance variables: belong to specific instance (each object has its own copy), defined in init (self.name = name), different for each instance. Class variables: shared across all instances (one copy for all objects), defined at class level, same for every instance. Use instance variables for: data unique to each instance (user.name, user.email—each user has different values). Use class variables for: data shared by all instances (User.user_count—total users, same for all), constants (Math.PI—doesn't change per instance). Common mistake: modifying class variable (User.count += 1) from instance (user1.count += 1—creates instance variable shadowing class variable). Gotcha: mutable class variables (class User: items = []—all instances share same list—dangerous). Always use instance variables for mutable data.

init is constructor—runs automatically when creating instance. Sets up initial state (instance variables, required setup). Example: class User: def init(self, name): self.name = name. Now User('Bob') creates user with name='Bob'. Without init: can create instances but they have no initial data (need to manually set every attribute). Use init to: initialize instance variables (self.name = name), validate input (raise error if invalid), perform setup (open database connection, load configuration). self is instance reference (like 'this' in other languages)—always first parameter. Don't: perform slow operations in init (makes object creation slow), have complex logic (keep init simple, delegate to methods). Do: set instance variables, validate required parameters, keep it fast.

Instance method: operates on specific instance (self parameter), accesses instance data (self.name). Example: def get_name(self): return self.name. Class method: operates on class itself (@classmethod decorator, cls parameter), accesses class variables. Example: @classmethod def count_users(cls): return cls.user_count. Static method: no access to instance or class (@staticmethod), just function grouped with class for organization. Example: @staticmethod def validate_email(email): return '@' in email. Use instance methods for: operations on specific object (user.save(), car.start()). Use class methods for: factory methods (User.from_json(data)—creates User from JSON), operations on class itself. Use static methods for: utilities related to class (User.validate_password(pwd)). Most common: instance methods (90%+), occasional class methods, rare static methods.

Inheritance: create new class based on existing class (subclass inherits parent's methods/attributes). Example: class Animal: def speak(self). class Dog(Animal): def speak(self): return 'Woof'. Dog inherits from Animal, overrides speak(). Use inheritance when: is-a relationship (Dog is-a Animal), code reuse (shared behavior across similar classes), extending existing classes. Don't use when: has-a relationship (Car has-a Engine—use composition: class Car: self.engine = Engine()), unrelated classes (forcing inheritance for code reuse creates confusing hierarchies). Python supports multiple inheritance but use sparingly (complex, confusing). Composition often better: class Car: def init(self): self.engine = Engine()—clearer than inheritance. Inheritance is powerful but overused. Start with simple classes, add inheritance only when clear is-a relationship exists.

Automate Your IT Operations

Leverage automation to improve efficiency, reduce errors, and free up your team for strategic work.