Raymond Hettinger «Build powerful, new data structures with Python's abstract base classes»

Видео с мероприятий {speach! · 37K views · 2019 · 1:02:01

Open on YouTube

Python, Ruby & scripting languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining abstract base classes, MutableMapping and MutableSequence, and how to build custom containers cheaply.

Recommended by 3 people across 5 different months, 2020 to 2022.

What it is

A PyCon Russia 2019 conference talk by Python core developer Raymond Hettinger on using the collections.abc abstract base classes to build new data structures. It includes worked examples such as a file-backed, persistent dict built by subclassing MutableMapping.

What people said

Recommends Reblochon Masque · 2020-05 · Stack Overflow · on “class-based dictionary in python using pickle”

You should probably have your `FileDict` inherit from `collections.abc.MutableMapping` and get most dictionary methods for free: MutableMapping [LINK] - I also recommend this video from @RaymondH that specifically talks about subclassing a python dict to make a permanent one using files: [this video]

Recommends Reblochon Masque · 2020-11 · Stack Overflow · on “How to modify a preexisting class in Python in order to add custom methods?”

Let the master explain this to you: Raymond Hettinger «Build powerful, new data structures with Python's abstract base classes» [this video]

Recommends Copperfield · 2020-12 · Stack Overflow · on “Python can't handle a very large list”

take a look at the filedict/sqldict code example from this video [this video] , it might be of your interest to move your stuff from memory to disk and back in a easy way

Recommends Alex Waygood · 2021-08 · Stack Overflow · on “How to implement MutableSequence on custom class”

collections.UserList is a good example [LINK] of how to properly implement the collections.abc.MutableSequence API. Check out this [this video] by Raymond Hettinger on Python's Abstract Base Classes and how to use them. The typing.overload decorator is irrelevant to the runtime implementation of a function. Its sole purpose is to aid static type-checking. Good blog on how/when to use it here [LINK] .

Recommends Alex Waygood · 2021-09 · Stack Overflow · on “Determine if subclass has a base class's method implemented in Python”

I'm not sure I understand correctly, but it sounds like you might be looking for Abstract Base Classes. (Documentation here [LINK] , tutorial here [this video] .) If you specify an abstractmethod in a base class that inherits from abc.ABC , then attempting to instantiate a subclass will fail unless that subclass overrides the abstractmethod. from abc import ABC, abstractmethod class Base(ABC): @abstractmethod def fail(self): pass class Task(Base): pass class Task2(Base): def fail(self): pass # this raises an exception # `fail` method has not been overridden in the subclass. t1 = Task() # this succeeds # `fail` method has been overridden in the subclass. t2 = Task2() If you want a check to happen

Cites Alex Waygood · 2021-09 · Stack Overflow · on “How to hint at number *types* (i.e. subclasses of Number) - not numbers themselves?”

In general, how do we hint classes , rather than instances of classes ? In general, if we want to tell a type-checker that any instance of a certain class (or any instance of a subclass of that class) should be accepted as an argument to a function, we do it like so: def accepts_int_instances(x: int) -> None: pass class IntSubclass(int): pass accepts_int_instances(42) # passes MyPy (an instance of `int`) accepts_int_instances(IntSubclass(666)) # passes MyPy (an instance of a subclass of `int`) accepts_int_instances(3.14) # fails MyPy (an instance of `float` — `float` is not a subclass of `int`) Try it on MyPy playground here! [LINK] If, on the other hand, we have a class C , and we want to h

Cites Alex Waygood · 2021-09 · Stack Overflow · on “Python init object of generic type”

As @user2357112supportsMonica says in the comments, typing.Generic is pretty much only there for static analysis, and has essentially no effect at runtime under nearly all circumstances. From the look of your code, it looks like what you're doing might be better suited to Abstract Base Classes (documentation here [LINK] , tutorial here [this video] ), which can be easily combined with Generic . A class that has ABCMeta as its metaclass is marked as an Abstract Base Class (ABC). A subclass of an ABC cannot be instantiated unless all methods in the ABC marked with the @abstractmethod decorator have been overridden. In my suggested code below, I've explicitly added the ABCMeta metaclass to your Seri

Cites adam.hendry · 2022-09 · Stack Overflow · on “Type annotations for *args and **kwargs”

I'm trying out Python's type annotations with abstract base classes to write some interfaces. Is there a way to annotate the possible types of *args and **kwargs ... How does one annotate the sensible types for *args and **kwargs There are two general usage categories when it comes to type hinting: Writing your own code ( which you can edit and change ) Using 3rd party code ( which you can't edit, or is hard to change ) Most users have some combo of both. The answer depends on whether your *args and **kwargs have homogeneous types (i.e. all of the same type) or heterogenous types (i.e. different types), as well as whether there is a fixed number of them or a variable/indeterminate number of