惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
博客园 - 聂微东
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
F
Fortinet All Blogs
H
Help Net Security
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
L
LangChain Blog
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Python for Beginners — Part 8: Object-Oriented Python
Ramesh S · 2026-06-23 · via DEV Community

Ramesh S

Part 8 of an 8-part beginner-friendly series on learning Python from scratch.

Congratulations — you've made it to the final part!

Everything you've learned — variables, control flow, collections, functions, file I/O, error handling — can be organized into classes and objects. This is object-oriented programming (OOP), and it's how professional Python code is written.

OOP isn't required to write working programs, but it's essential for writing code that scales, is maintainable, and professional. This final article brings everything together.

Classes and Objects

A class is a blueprint for creating objects. An object is an instance of that class. Think of a class like a cookie cutter and objects like the cookies it produces.

Defining a class

class Person:
    pass

# Create an object (instance)
person = Person()
print(person)  # <__main__.Person object at 0x...>

class Person: defines a class. person = Person() creates an object from that class.

The __init__ Method and self

The __init__ method runs automatically when you create an object. It's where you initialize the object's data.

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

person = Person("Ramesh", 25)
print(person.name)  # Ramesh
print(person.age)   # 25

self represents the object itself. When you write self.name = name, you're saying "this object's name attribute equals the name I was given."

Think of it this way:

  • name (parameter) is the input
  • self.name (attribute) is the object's stored data

Creating multiple objects

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

person1 = Person("Ramesh", 25)
person2 = Person("Priya", 23)

print(person1.name, person1.age)  # Ramesh 25
print(person2.name, person2.age)  # Priya 23

Each object is independent. person1.name is separate from person2.name.

Methods

A method is a function inside a class. Methods operate on the object's data:

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

    def greet(self):
        return f"Hello, I'm {self.name}"

    def have_birthday(self):
        self.age += 1

person = Person("Ramesh", 25)
print(person.greet())       # Hello, I'm Ramesh
person.have_birthday()
print(person.age)           # 26

Call methods with dot notation: person.greet(). The self parameter is passed automatically — you don't type it.

Instance vs Class Variables

Instance variables are unique to each object. Class variables are shared by all objects of that class:

class Dog:
    species = "Canis familiaris"  # Class variable

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

dog1 = Dog("Buddy")
dog2 = Dog("Max")

print(dog1.name)           # Buddy (unique to dog1)
print(dog2.name)           # Max (unique to dog2)
print(Dog.species)         # Canis familiaris (shared)
print(dog1.species)        # Canis familiaris (accessible via instance too)

Use class variables for data shared by all instances. Use instance variables for data unique to each instance.

Inheritance

Inheritance lets you create a new class based on an existing class. The new class inherits methods and attributes from the parent:

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

    def speak(self):
        return f"{self.name} makes a sound"

class Dog(Animal):
    def speak(self):
        return f"{self.name} barks"

class Cat(Animal):
    def speak(self):
        return f"{self.name} meows"

dog = Dog("Buddy")
cat = Cat("Whiskers")

print(dog.speak())  # Buddy barks
print(cat.speak())  # Whiskers meows

Dog and Cat inherit from Animal and override the speak() method. This is called method overriding.

Using super()

Call the parent class's method with super():

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

    def describe(self):
        return f"{self.name} is an animal"

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)  # Call parent's __init__
        self.breed = breed

    def describe(self):
        parent_desc = super().describe()  # Call parent's describe
        return f"{parent_desc} and is a {self.breed}"

dog = Dog("Buddy", "Golden Retriever")
print(dog.describe())  # Buddy is an animal and is a Golden Retriever

super() lets you access the parent class without repeating code.

Polymorphism

Polymorphism means "many shapes" — the same function name works differently depending on the object type:

class Dog:
    def speak(self):
        return "Woof!"

class Cat:
    def speak(self):
        return "Meow!"

class Cow:
    def speak(self):
        return "Moo!"

animals = [Dog(), Cat(), Cow()]

for animal in animals:
    print(animal.speak())

# Output:
# Woof!
# Meow!
# Moo!

Each animal speaks differently, but you call the same method. This is polymorphism in action.

Encapsulation

Encapsulation is the idea of bundling data and methods together, and controlling access to them. Python uses naming conventions:

  • public_attribute — accessible from outside (convention)
  • _private_attribute — intended private (convention, not enforced)
  • __really_private — name-mangled, harder to access (enforced)
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # "Private"

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount

    def withdraw(self, amount):
        if 0 < amount <= self.__balance:
            self.__balance -= amount

    def get_balance(self):
        return self.__balance

account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())  # 1500

# Can't access directly:
# print(account.__balance)  # AttributeError (name-mangled)

The double underscore (__) makes the attribute private. Access it through methods instead. This prevents accidental misuse.

Dunder Methods

Dunder methods (double underscore) are special methods Python calls automatically. Here are the most important:

__str__ and __repr__

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

    def __str__(self):
        return f"{self.name} ({self.age})"

    def __repr__(self):
        return f"Person('{self.name}', {self.age})"

person = Person("Ramesh", 25)
print(str(person))   # Ramesh (25) — human-readable
print(repr(person))  # Person('Ramesh', 25) — for debugging

__str__ is for end users. __repr__ is for developers.

__len__

class ShoppingCart:
    def __init__(self):
        self.items = []

    def add(self, item):
        self.items.append(item)

    def __len__(self):
        return len(self.items)

cart = ShoppingCart()
cart.add("apple")
cart.add("banana")
print(len(cart))  # 2

Now len(cart) returns the number of items.

__eq__

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

    def __eq__(self, other):
        return self.name == other.name

person1 = Person("Ramesh")
person2 = Person("Ramesh")
print(person1 == person2)  # True (compares names)

Define how == works for your objects.

Properties and Getters/Setters

Properties let you access attributes like data, but with method-like control:

class Person:
    def __init__(self, name):
        self._name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if len(value) > 0:
            self._name = value

person = Person("Ramesh")
print(person.name)     # Ramesh (getter)
person.name = "Priya"  # Setter
print(person.name)     # Priya

The @property decorator makes name act like an attribute but run validation code.

Practical Examples

Example 1: User class

class User:
    def __init__(self, username, email):
        self.username = username
        self.email = email
        self.created_at = datetime.now()

    def __str__(self):
        return f"User({self.username})"

    def change_email(self, new_email):
        if "@" in new_email:
            self.email = new_email
        else:
            raise ValueError("Invalid email")

user = User("ramesh", "ramesh@example.com")
print(user)  # User(ramesh)
user.change_email("ramesh.new@example.com")

Example 2: Inheritance with Bank Accounts

class Account:
    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

class SavingsAccount(Account):
    def __init__(self, owner, balance, interest_rate):
        super().__init__(owner, balance)
        self.interest_rate = interest_rate

    def apply_interest(self):
        self.balance *= (1 + self.interest_rate)

savings = SavingsAccount("Ramesh", 1000, 0.05)
savings.deposit(500)
savings.apply_interest()
print(savings.balance)  # 1575.0

Example 3: Polymorphism with shapes

class Shape:
    def area(self):
        raise NotImplementedError

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius ** 2

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
    print(f"Area: {shape.area()}")

# Output:
# Area: 78.5
# Area: 24

Why This Matters

OOP is how professional code is organized. Without it, programs become tangled messes of functions and variables. With OOP, you group related data and behavior together into classes, making code:

  • Reusable — inheritance lets you extend existing code
  • Maintainable — encapsulation hides complexity
  • Scalable — polymorphism lets you add new types easily
  • Clear — objects represent real-world concepts

The most common beginner mistakes:

  • Forgetting self in methods
  • Confusing __init__ (constructor) with class definition
  • Not using inheritance when it makes sense
  • Overcomplicating with too many classes (start simple)
  • Breaking encapsulation with direct attribute access

Your Journey is Complete

You've now learned:

  1. Syntax and basics (Parts 1–2)
  2. Working with text and logic (Parts 3–4)
  3. Organizing data (Part 5)
  4. Organizing code (Part 6)
  5. Working in the real world (Part 7)
  6. Professional code structure (Part 8: OOP)

You have all the fundamental skills of a Python programmer. Where you go from here is up to you — web development, data science, automation, games, machine learning — Python is everywhere.

What's Next

You've completed the 8-part beginner series. From here:

  • Web development: Flask or Django frameworks
  • Data science: Pandas, NumPy, Matplotlib
  • Automation: Scripts to automate tasks
  • Games: Pygame library
  • Machine learning: Scikit-learn, TensorFlow
  • Advanced Python: Decorators, metaclasses, async/await

The fundamentals you've learned here apply to everything. Pick a project that excites you and build it.