Quark’s Outlines: Python Classes

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Classes

What is a Python class?

You use Python classes to define new kinds of objects. A Python class is like a mold. It shapes how objects are made and how they act. When you make a class, you create a plan for future things.

When you call a class like a function, Python creates a new object. This object is called a class instance. The class sets the rules for how that object behaves. A class can have data and actions. The data is called attributes. The actions are called methods.

Python lets you define a class to group data and actions.

class Person:
    def greet(self):
        print("Hello")

p = Person()
p.greet()
# prints: Hello

How do Python classes store and share data?

When you define a class, Python creates a dictionary to store the class’s data. This is called the class namespace. Any value you assign to the class goes into that dictionary.

If you access a value from a class and it is not found, Python looks in the base classes. This lookup goes from left to right, top to bottom, in the order you wrote them.

Python lets you assign values to a class with simple rules.

class Shape:
    sides = 4

print(Shape.sides)
# prints: 4

Python will not copy values from a base class to the new class. It only looks them up if needed.

What happens when you call a Python class?

You call a Python class like a function. This makes a new instance. If your class defines a method called __init__, Python runs that method when you create the object.

If the class has no __init__ method, Python creates the object without setup.

Python runs the init method when you create a new object.

class Box:
    def __init__(self, label):
        self.label = label

b = Box("toys")
print(b.label)
# prints: toys

The __init__ method gives the new object its first values. You can pass in arguments, and Python sends them to __init__.

What attributes do Python classes have?

Each Python class has special attributes. These attributes are part of the language and always exist:

  • __dict__ holds the class’s data in a dictionary
  • __name__ is the class’s name
  • __bases__ is a tuple of the class’s base classes
  • __doc__ is the documentation string, or None if not written

These attributes help you inspect the class while your program runs.

Python gives each class a set of built-in attributes.

class Tool:
    "A simple tool class"

print(Tool.__name__)
# prints: Tool
print(Tool.__doc__)
# prints: A simple tool class

These built-in values are read-only. You can view them but not change most of them.

A Historical Timeline of Python Classes

Where do Python class rules come from?

Python classes were designed to be easy to use and flexible. Python chose not to use symbols like {} for blocks and instead relied on whitespace. These choices made Python classes easier to read than many older languages.

People invented ways to group actions and data

1967 — Simula 67 defines classes

Simula introduced classes as part of the first object-oriented language.

1979 — Smalltalk uses message-based methods

Smalltalk helped shape how objects call methods using dot notation.

1983 — C++ supports class inheritance

C++ showed how classes can share traits using base and derived classes.

People designed Python’s object system

1991 — Python 0.9.0 includes user-defined classes

Python added classes early and supported __init__, inheritance, and method access.

2000 — Python 2.2 adds new-style classes

New-style classes gave all classes a single object model and enabled properties and descriptors.

People expanded and refined Python classes

2003 — super() added to simplify base class calls

Python added a way to call methods from base classes using super().

2010 — __slots__ feature added for smaller instances

Python introduced slots to control memory use and limit dynamic attributes.

2017 — dataclasses added for simpler class declarations

Python 3.7 added a module to define data-focused classes with less code.

Problems & Solutions with Python Classes

How do you use Python classes the right way?

You use Python classes to make new types that hold both data and actions. These types help you organize code and reuse logic. The problems below show how to solve daily coding needs using Python class rules.

Problem: How do you group data and give it actions in Python?

You are writing a game. Each player has a name and a way to say hello. You want one kind of object that holds the name and knows how to greet. You do not want to write the greeting logic more than once.

Problem: How can you group data and logic into one object?

Solution: Define a Python class. Add attributes for the data and methods for the actions.

Python lets you build reusable objects with classes.

class Player:
    def __init__(self, name):
        self.name = name
    def greet(self):
        print(f"Hi, I’m {self.name}!")

p = Player("Alex")
p.greet()
# prints: Hi, I’m Alex!

The class holds both the name and the greeting behavior.

Problem: How do you make new objects from a base plan in Python?

You are building a web app. You want a base class for all users, but also special classes for admins and guests. Each type shares some behavior but has its own setup.

Problem: How can you share code between related types?

Solution: Use Python class inheritance. Put common logic in a base class. Make new classes that extend it.

Python lets you reuse code with class inheritance.

class User:
    def login(self):
        print("User logged in.")

class Admin(User):
    def login(self):
        print("Admin access granted.")

a = Admin()
a.login()
# prints: Admin access granted.

The Admin class overrides the login method but still acts like a User.

Problem: How do you track state for each object in Python?

You are writing a tool that counts items. Each counter needs its own value. You want a way to store and update the value inside each object.

Problem: How can you give each object its own copy of a variable?

Solution: Use self inside a Python class to store values on each object.

Python lets each object hold its own data using attributes.

class Counter:
    def __init__(self):
        self.count = 0
    def add(self):
        self.count += 1
    def show(self):
        print(self.count)

c = Counter()
c.add()
c.add()
c.show()
# prints: 2

Each counter tracks its own value using self.count.

Problem: How do you inspect what a class holds in Python?

You write a class and later want to see what attributes it has. You also want to know its base classes. You are debugging and want a built-in way to check the structure.

Problem: How do you see what Python stored in a class in Python?

Solution: Use Python’s special attributes like __dict__, __name__, and __bases__.

Python gives each class built-in attributes for inspection.

class Pet:
    pass

print(Pet.__name__)
# prints: Pet
print(Pet.__dict__)
# prints: {'__module__': '__main__', '__dict__': ..., ...}

You can use these to understand how Python built your class.

Problem: How do you control how new objects are set up in Python?

You are writing a program that tracks books. You want each book to store a title and author. You want to make sure every book starts with those values in place.

Problem: How do you make sure each object is initialized with the right data in Python?

Solution: Define a __init__ method. Python will call it when the class is used to make a new object.

Python lets you customize object creation with init.

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

b = Book("1984", "George Orwell")
print(b.title, "-", b.author)
# prints: 1984 - George Orwell

Now each book object has its own title and author set at the start.

Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. I’d love to hear your thoughts in the comments, too! If you want to see more content like this, don’t forget to subscribe. Thanks for reading!

Mike Vincent is an American software engineer and app developer from Los Angeles, California. More about Mike Vincent

Leave a Reply