In OOP (Object Oriented Programming), a base class that cannot be instantiated because some of its declared methods lack a definition, which is intended to be provided by derived classes. The term may have more specific or slightly different meaning according to the particular computer language involved.
Questions tagged [abstract-base-class]
109 questions
6
votes
1 answer
Why won't mypy understand this object instantiation?
I'm trying to define a class that takes another class as an attribute _model and will instantiate objects of that class.
from abc import ABC
from typing import Generic, TypeVar, Any, ClassVar, Type
Item = TypeVar("Item", bound=Any)
class…

jpmelos
- 3,283
- 3
- 23
- 30
6
votes
6 answers
How to declare a member in a base template class where the type is dependent of the derived class?
Given a base class using CRTP, I'm looking at declaring a member in the base template class where the type is dependent of the derived class.
While the following works as intended:
template class BaseTraits;
template class Base…

Flyer
- 327
- 2
- 14
6
votes
1 answer
Do we have a better way of returning abstract classes in C++?
I want to start throwing some interfaces into my C++ code to make it easier for me to unit test using mocks.
The problem with this is returning abstract classes from a method in C++ is a pain. You can't return by value so you need to return a…

user1217210
- 113
- 5
6
votes
1 answer
issubclass of abstract base class Sequence
This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__, __len__, __contains__, __iter__, __reversed__, index, and count. So why does this minimal implementation does not work, i.e. why…

Kijewski
- 25,517
- 12
- 101
- 143
6
votes
2 answers
Python ABCs: registering vs. subclassing
(I am using python 2.7) The python documentation indicates that you can pass a mapping to the dict builtin and it will copy that mapping into the new dict:
http://docs.python.org/library/stdtypes.html#mapping-types-dict
I have a class that…

Eric Snow
- 1,198
- 8
- 21
6
votes
2 answers
Creating a new pointer to a pure virtual class
I have a class of the following design:
class Meal {
public:
virtual void cook() = 0; // pure virtual
}
class Omelette : Meal {
public:
void cook() {/*do something*/}; // non-virtual
}
class Waffle : Meal {
public:
void cook() {/*do…

nwn
- 583
- 7
- 23
6
votes
3 answers
Implement two functions with the same name but different, non-covariant return types due to multiple abstract base classes
If I have two abstract classes defining a pure virtual function with the same name, but different, non-covariant return types, how can I derive from these and define an implementation for both their functions?
#include
class ITestA {
…

Tar
- 276
- 2
- 10
5
votes
1 answer
Python type annotations for instance of class derived from abstract base class
Assume that an abstract base class MembershipClass has been created. Multiple classes are derived from the abstract base class, e.g., FirstClass, SecondClass, etc.
I wish to use type annotations in a function that accepts as an argument any class…

rhz
- 960
- 14
- 29
5
votes
1 answer
In Python, how to know whether objects can be compared?
With abstract base classes, Python provides a way to know the behavior of objects without actually trying it out. In the standard library, we have some ABCs defined for containers in collections.abc. For example, one can test that an argument is…

Francis Colas
- 3,459
- 2
- 26
- 31
4
votes
2 answers
Generic abstract base class for singletons (C#) - cannot instantiate the private Lazy instance
I currently have a collection of 6 or 7 singletons, all of which do almost the same thing (see the For method in the below example) but with a different internal DB query and return a collection of different objects (so parsing the DB results is…

awj
- 7,482
- 10
- 66
- 120
4
votes
1 answer
What is the best way to display in a view more types that are derived from an abstract base?
For example I have these three classes:
public abstract class AbstractBase
{
public int A { set; get; }
}
public class Derived1 : AbstractBase
{
public int B { set; get; }
public int C { set; get; }
}
public class Derived2 :…

dvjanm
- 2,351
- 1
- 28
- 42
4
votes
1 answer
Change model to inherit from abstract base class without changing DB
I have a simple model for a product that looks like this:
class Product(models.Model):
name = models.CharField(max_length=80)
# other attributes
We already have this rolled out, and have a DB with these fields filled out. I want to change…

user1496984
- 10,957
- 8
- 37
- 46
3
votes
1 answer
How to avoid duplicating arguments definition when inheriting from abstract class?
I have an abstract class written in Python:
class AbsctracClass(ABC):
@abstractmethod
def method(self,
value1: int,
value2: float,
value3: str,
value4: Optional[list] = None,
value5: Optional[int] =…

Mihail Kondratyev
- 255
- 3
- 10
3
votes
1 answer
Access Type Hints for attributes of a dataclass created in post_init
Python: 3.7+
I have a dataclass and a subclass of it as following:
from abc import ABC
from dataclasses import dataclass
from typing import Dict, List, Optional
from dbconn import DBConnector
@dataclass
class User:
uid: int
name:…

Harshith Thota
- 856
- 8
- 20
3
votes
1 answer
The Python Generator abstract base class doesn't implement the necessary __del__, is this intentional?
Generator objects in Python are required to have a close method that exists to ensure that context managers are exited and try...finally: blocks are run before the object is garbage collected.
PEP 342 defines the methods send,throw,close and __del__…

MattMcEwen
- 73
- 7