Linked Questions
18 questions linked to/from Why use Abstract Base Classes in Python?
12 votes
0 answers
8k views
What is the difference between Abstract Classes and Metaclasses in python? [duplicate]
I read two articles about Metaclassing in python: What is a metaclass in Python? http://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/ And I read about the ABC(abstract base ...
-1 votes
1 answer
513 views
practical example of defining using metaclass like setting it to ABCMeta or LoggingType [duplicate]
Example of setting metaclass to LoggingType i spotted at my workplace. import logging as _logging class SomeClass(object): __metaclass__ = _logging.LoggingType Here is the example I have seen for ...
0 votes
1 answer
301 views
Why are abstract classes that do nothing used? [duplicate]
I was tinkering with an example of the strategy pattern that can be found below. from __future__ import annotations from abc import ABC, abstractmethod from typing import List class Context(): ...
70 votes
2 answers
81k views
Class accessible by iterator and index
My class implements an iterator so I can do: for i in class(): But I want to be able to access the class by index as well: class()[1] How can I do that?
10 votes
3 answers
24k views
Python's super(), abstract base classes, and NotImplementedError
Abstract base classes can still be handy in Python. In writing an abstract base class where I want every subclass to have, say, a spam() method, I want to write something like this: class Abstract(...
22 votes
1 answer
6k views
Using abstract base class VS plain inheritance
I'm trying to understand the benefits of using abstract base classes. Consider these two pieces of code: Abstract base class: from abc import ABCMeta, abstractmethod, abstractproperty class CanFly:...
5 votes
4 answers
1k views
Right way to extend list
I want to extend functionality of class "list" and add custom handlers for events: "Add new item to list" and "Remove item from list". For this task I don't want to use ...
5 votes
1 answer
4k views
No error while instantiating abstract class, even though abstract method is not implemented
I was trying out the below python code: from abc import ABCMeta, abstractmethod class Bar: __metaclass__ = ABCMeta @abstractmethod def foo(self): pass class Bar2(Bar): def ...
3 votes
2 answers
3k views
What is the best way to allow only one of a pair of arguments in a Python class constructor?
I have a class which essentially functions like the example below. The constructor takes two arguments, which are both defaulted to None if no value is given. The goal of the class is that, given a ...
1 vote
4 answers
694 views
Trouble using base class to create instances of its child classes
Below is a simplified version of the code I have: class Base: def __new__(klass, *args): N = len(args) try: return [Sub0, Sub1, Sub2][N](*args) except ...
1 vote
2 answers
1k views
Python: Working with configparser inside a class. How to access my attributes names and values dynamically inside my class-methods
My first program is getting much bigger than excepted. :) import configparser config = configparser.ConfigParser() configfile = 'RealGui.ini' class FolderLeftSettings: def __init__(...
3 votes
1 answer
782 views
Why is it important to prevent instantiation of classes that are theoretically abstract?
I have two data structures, Frontier (a queue) and Explored (a set). I want to implement a custom __contains__ method that they share: class Custom_Structure: def __contains__(self, item): ...
0 votes
1 answer
357 views
How exactly does Python ABC interface work?
I was reading the book Fluent Python by Luciano Ramalho and I encountered the following line: Note that the built-in concrete sequence types do not actually subclass the Sequence and MutableSequence ...
2 votes
1 answer
735 views
Why are the .copy and .clear methods not part of the specifications for sequence abstract base classes?
EDIT: could it be that this is just an oversight that has not been addressed? The standard types documentation includes .copy() and .clear() in the table of methods for mutable sequence types. I ...
0 votes
2 answers
448 views
pythonic way to expose user override hooks
Note: although my particular use is Flask related, I think the question is more general. I am building a Flask web application meant to be customized by the user. For example, the user is expected ...