abstract-base-class

issubclass of abstract base class Sequence

余生颓废 提交于 2019-12-18 08:57:30
问题 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 issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self

In Python, how to know whether objects can be compared?

本秂侑毒 提交于 2019-12-14 01:21:38
问题 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 iterable: from collections.abc import Iterable def function(argument): if not isinstance(argument, Iterable): raise TypeError('argument must be iterable.') # do stuff with the argument I was hoping that there would be one such ABC for deciding whether

Why is __slots__ behaving differently in Python 2 and 3 when inheriting from an abstract base class

荒凉一梦 提交于 2019-12-11 03:53:06
问题 I created the following class to store changeable points on a plane in a memory-efficient manner - I need a mutable equivalent of namedtuple('Point', 'x y') . Since instance dictionaries are big, I thought I'd go for __slots__ : from collections import Sequence class Point(Sequence): __slots__ = ('x', 'y') def __init__(self, x=0, y=0): self.x = x self.y = y def __getitem__(self, item): return getattr(self, self.__slots__[item]) def __setitem__(self, item, value): return setattr(self, self._

Implement two functions with the same name but different, non-covariant return types due to multiple abstract base classes

六眼飞鱼酱① 提交于 2019-12-07 03:16:34
问题 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 <iostream> class ITestA { public: virtual ~ITestA() {}; virtual float test() =0; }; class ITestB { public: virtual ~ITestB() {}; virtual bool test() =0; }; class C : public ITestA, public ITestB { public: /* Somehow implement ITestA::test and ITestB::test */ }; int main() {

Python ABCs: registering vs. subclassing

做~自己de王妃 提交于 2019-12-04 12:47:09
问题 (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 implements the Mapping ABC, but it fails: import collections class Mapping(object): def __init__(self, dict={}): self.dict=dict def __iter__(self): return iter(self.dict) def __iter__(self): return iter(self.dict) def __len__(self): return len(self.dict)

Python ABCs: registering vs. subclassing

旧街凉风 提交于 2019-12-03 08:05:01
(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 implements the Mapping ABC, but it fails: import collections class Mapping(object): def __init__(self, dict={}): self.dict=dict def __iter__(self): return iter(self.dict) def __iter__(self): return iter(self.dict) def __len__(self): return len(self.dict) def __contains__(self, value): return value in self.dict def __getitem__(self, name): return self.dict

C++ Calling a copy constructor on an unknown derived class through an abstract base class

巧了我就是萌 提交于 2019-11-30 08:25:31
问题 I'm making a tree that has several different node types: a binary node, a unary node, and a terminal node. I've got an ABC that all the nodes inherit from. I'm trying to write a recursive copy constructor for the tree like so: class gpnode { public: gpnode() {}; virtual ~gpnode() {}; gpnode(const gpnode& src) {}; gpnode* parent; } class bnode:gpnode { public: bnode() {//stuff}; ~bnode() {//recursive delete}; bnode(const bnode& src) { lnode = gpnode(src.lnode); rnode = gpnode(src.rnode); lnode

issubclass of abstract base class Sequence

对着背影说爱祢 提交于 2019-11-29 15:21:45
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 issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self): return self def index(self, item): raise IndexError def count(self, item): return 0 issubclass(S,

C++ Calling a copy constructor on an unknown derived class through an abstract base class

感情迁移 提交于 2019-11-29 06:12:50
I'm making a tree that has several different node types: a binary node, a unary node, and a terminal node. I've got an ABC that all the nodes inherit from. I'm trying to write a recursive copy constructor for the tree like so: class gpnode { public: gpnode() {}; virtual ~gpnode() {}; gpnode(const gpnode& src) {}; gpnode* parent; } class bnode:gpnode { public: bnode() {//stuff}; ~bnode() {//recursive delete}; bnode(const bnode& src) { lnode = gpnode(src.lnode); rnode = gpnode(src.rnode); lnode->parent = this; rnode->parent = this; } gpnode* lnode; gpnode* rnode; } class unode:gpnode { public:

Can a Python Abstract Base Class enforce function signatures?

妖精的绣舞 提交于 2019-11-28 02:48:37
问题 Suppose I define an abstract base class like this: from abc import abstractmethod, ABCMeta class Quacker(object): __metaclass__ = ABCMeta @abstractmethod def quack(self): return "Quack!" This ensures any class deriving from Quacker must implement the quack method. But if I define the following: class PoliteDuck(Quacker): def quack(self, name): return "Quack quack %s!" % name d = PoliteDuck() # no error I'm allowed to instantiate the class because I've provided the quack method, but the