Reference library

OOP & classes

Classes, instances, methods, dataclasses, and object-oriented design in Python.

4 matches
OOP & classes easy

How to Convert Data Types in Python with a Helper Class

This code defines a beginner-friendly OOP helper class for common data conversions like string to list, list to dict, JSON string, and CSV row, with an advanced subclass for numeric casting.

oop classes data-conversion
Python
class DataConverter:
    """A beginner-friendly helper class for common data conversions."""
    
    def __init__(self, data):
        self.data = data
    
    def to_list(self):
        """Convert string data (comma-separated) to a list."""
        if isinstance(self.data, str):
            return [item.strip() for…
14 0 Open
OOP & classes easy

How to Create a Data Formatter Class in Python

A beginner-friendly helper class to format lists, dictionaries, and stored records into readable strings.

oop class formatting
Python
class DataFormatter:
    """Helper class for beginners to format common data types."""
    
    def __init__(self, name="data"):
        self.name = name
        self.records = []
    
    def add_record(self, key, value):
        """Add a key-value record to the formatter."""
        self.records.append({"key": key, …
12 0 Open
OOP & classes easy

How to Sort Data in Python with a Class Helper

This beginner-friendly class wraps the built-in sorted() function to sort numbers, strings ignoring case, and dictionaries by a specified key.

oop sorting sorted
Python
class DataSorter:
    def __init__(self, data):
        self.data = data

    def sort_numbers(self, reverse=False):
        return sorted(self.data, reverse=reverse)

    def sort_strings_ignore_case(self, reverse=False):
        return sorted(self.data, key=str.lower, reverse=reverse)

    def sort_dicts_by_key(self…
14 0 Open
OOP & classes easy

How to Use StrEnum with auto() in Python

Define string-valued enum members automatically by using StrEnum with the auto() helper, making each member's value its own uppercase name.

enum strenum auto
Python
from enum import StrEnum, auto

class Color(StrEnum):
    RED = auto()
    GREEN = auto()
    BLUE = auto()

class Language(StrEnum):
    PYTHON = auto()
    JAVASCRIPT = auto()
    RUST = auto()

print(list(Color))
print(list(Language))

print(Color.RED == "RED")
print(Language.PYTHON == "PYTHON")

print(f"Color: {Co…
13 0 Open

Browse by section

Each section groups closely related Python snippets.

OOP & classes — Python code examples

What you will find here

This page collects oop & classes snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.