4 Oop — Python 3 Deep Dive Part

Use @abstractmethod with @classmethod, @staticmethod, @property.


Attribute lookup in Python goes through several steps:

To optimize:

| Issue | Details | |-------|---------| | Length | ~30+ hours of video + exercises. Can feel overwhelming if you're on a tight schedule. | | Pacing | Some sections (e.g., descriptors) are extremely detailed. You might need to rewatch or pause often. | | Prerequisites | You must know Python functions, closures, decorators, and basic classes. Not for first-time programmers. | | Light on async/await | This is OOP-specific; no asyncio coverage (that's in Part 5). |


class Temperature:
    def __init__(self, celsius):
        self._celsius = celsius
@property
def celsius(self):
    return self._celsius
@celsius.setter
def celsius(self, value):
    if value < -273.15:
        raise ValueError("Too cold")
    self._celsius = value

# Fragile base class problem
class LoggedDict(dict):
    def __setitem__(self, key, value):
        print(f"Setting key=value")
        super().__setitem__(key, value)

Changes in dict in Python 3.7+ broke some subclasses relying on internal calls.

Python is duck-typed, but ABCs allow you to define interfaces and enforce method implementation.

class SingletonMeta(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super().__call__(*args, **kwargs)
        return cls._instances[cls]

class Database(metaclass=SingletonMeta): def init(self): print("Initializing DB") python 3 deep dive part 4 oop

db1 = Database() db2 = Database() print(db1 is db2) # True (singleton)

این مطالب رو هم پیشنهاد می‌کنیم ببینید

python 3 deep dive part 4 oop

درباره نویسنده: عصر اطلاعات

2 Comments

  1. دم شما گرم آپدیت کردم …بدون مشکل آپدیت میشه فقط بعد آپدیت دوستان باید در نظر داشته باشن که فکتوری ریست هم بزنن

نظرات بسته شده است.