DEV Community

Cover image for Python basics - Day 23
Sabin Sim
Sabin Sim

Posted on • Edited on

Python basics - Day 23

*Day 23 – Attributes & Methods *

Project: Build a “Car Factory App” to understand how attributes and methods work at both instance and class levels.


01. Learning Goal

By the end of this lesson, you will be able to:

  • Understand the difference between attributes and methods
  • Distinguish instance attributes from class attributes
  • Implement special methods (dunder methods) like __str__
  • Design reusable and structured classes using both types of attributes

02. Problem Scenario

You are developing a small “Car Factory” system.

Each car should have its own model and brand (instance attributes),

but all cars share the same number of wheels (class attribute).

You’ll also learn how to represent objects neatly using a special method.


*03. Step 1 – Attributes *

Attributes are variables that belong to an object.

They hold the object’s data and are accessed using the . operator.

class Person:
    def __init__(self, name, age):
        self.name = name   # Instance attribute
        self.age = age

p1 = Person("Sabin", 30)
print(p1.name)   # Sabin
print(p1.age)    # 30
Enter fullscreen mode Exit fullscreen mode

*04. Step 2 – Methods *

Methods are functions defined inside a class that usually operate on attributes.

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

    def bark(self):
        print(f"{self.name} is barking! / {self.name}가 짖고 있어요!")

d = Dog("Buddy")
d.bark()
Enter fullscreen mode Exit fullscreen mode

05. Step 3 – Instance vs Class Attributes

Attributes can exist at two levels:

  • Instance attribute → unique to each object
  • Class attribute → shared by all objects
class Student:
    school = "Python Academy"   # Class attribute

    def __init__(self, name):
        self.name = name        # Instance attribute

s1 = Student("Tom")
s2 = Student("Anna")

print(s1.name, s1.school)  # Tom Python Academy
print(s2.name, s2.school)  # Anna Python Academy
Enter fullscreen mode Exit fullscreen mode

06. Step 4 – Special Methods (Dunder Methods)

Special methods (also called dunder methods) begin and end with double underscores __.
__str__ defines how an object should be displayed as a string.

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

    def __str__(self):
        return f"'{self.title}' by {self.author}"

b = Book("Python Basics", "Sabin")
print(b)   # 'Python Basics' by Sabin
Enter fullscreen mode Exit fullscreen mode

07. Step 5 – Practice Examples

Example 1: Car Class

class Car:
    wheels = 4   # Class attribute

    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def info(self):
        print(f"{self.brand} {self.model} has {Car.wheels} wheels.")

c1 = Car("Tesla", "Model 3")
c1.info()   # Tesla Model 3 has 4 wheels.
Enter fullscreen mode Exit fullscreen mode

Example 2: Counter Class

class Counter:
    count = 0   # Class attribute

    def __init__(self):
        Counter.count += 1

c1 = Counter()
c2 = Counter()
print("Total objects:", Counter.count)  # 2
Enter fullscreen mode Exit fullscreen mode

08. Step 6 – Mini Project: Car Factory App

Let’s combine what we’ve learned into a small project.

class Car:
    factory_name = "Python Motors"  # Class attribute
    total_cars = 0                  # Class counter

    def __init__(self, brand, model):
        self.brand = brand
        self.model = model
        Car.total_cars += 1

    def info(self):
        print(f"{self.brand} {self.model} (Built by {Car.factory_name})")

    def __str__(self):
        return f"Car: {self.brand} {self.model}"

# Create cars
c1 = Car("Tesla", "Model 3")
c2 = Car("Hyundai", "Ioniq 6")

c1.info()
c2.info()
print("Total cars produced:", Car.total_cars)
Enter fullscreen mode Exit fullscreen mode

Output:

Tesla Model 3 (Built by Python Motors)
Hyundai Ioniq 6 (Built by Python Motors)
Total cars produced: 2
Enter fullscreen mode Exit fullscreen mode

09. Reflection

You have learned how to:

  • Define and use instance and class attributes
  • Implement and call methods from objects
  • Use dunder methods for readable string representation
  • Apply OOP principles in a real project (Car Factory example)

Next → Day 24 – Class Methods & Static Methods
Learn how to create methods that belong to the class itself rather than to individual objects.

Top comments (0)