Questions tagged [abc]

Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.

280 questions
2
votes
1 answer

Why does numbers.Real not have __gt__ as an abstract (or default) method?

Looking at the source code for numbers.py there is no @abstractmethod or implementation of __gt__ or __ge__. Why is this? Is this a bug? The documentation explicitly says: To Complex, Real adds the operations that work on real numbers. In short,…
bkanuka
  • 907
  • 7
  • 18
2
votes
1 answer

Implementing numbers.Real with minimal code

I'm trying to create a class MyFloat that is very similar to float but has a value method and a few other methods. I also need instances of this class to interact with real floats and be "sticky" in the sense that type(MyFloat(5) + 4) is MyFloat and…
bkanuka
  • 907
  • 7
  • 18
2
votes
1 answer

Defining @property.setter in Abstract Base Class gives AttributeError

An Abstract Base Class Base has a @abstractmethod named data which is also a @property. Question: Is there a way to define the property setter data.setter in the Base class, so that we don't have to repeatedly define the setter method in all the…
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
2
votes
1 answer

Inherit wrapper from abstract method

I would like to systematically wrap some overriden method of a base class. I am using ABC for the base class. I tried to wrap the @abstractmethod, putting the annotation before or after, but it doesn’t work. As I understand it, the the whole wrapped…
Silver Duck
  • 581
  • 1
  • 5
  • 18
2
votes
1 answer

Why doesn't the abc.ABCMeta abstract instantiation check work on derivatives of `list` and `dict`?

I have been experimenting a little with the abc module in python. A la >>> import abc In the normal case you expect your ABC class to not be instantiated if it contains an unimplemented abstractmethod. You know like as follows: >>> class…
LazyLeopard
  • 194
  • 1
  • 11
2
votes
1 answer

ABC class number from number module

ABC classes are created to check object type and as such they cannot be instantiated. In fact: basestring() throws: TypeError: The basestring type cannot be instantiated However, this does not happen for the ABC number: from numbers import…
Harry C.
  • 41
  • 1
  • 4
2
votes
1 answer

Difference between from collections import Container and from collections.abc import Container

We can import Container in two ways: from collections import Container from collections.abc import Container help function for both Container returns the same documentation. help(collections.Container): Help on class Container in module…
arshovon
  • 13,270
  • 9
  • 51
  • 69
2
votes
2 answers

How to use a function outside a class as a property inside a class?

I'm having some problems. How we can define a function outside of a function that can be used in a class property? Also, how we can insert the self parameter into the function signature? I would like to visualize it like this: >>> def a(self, x): …
Günel Resulova
  • 151
  • 1
  • 10
2
votes
0 answers

Why does the @abstractmethod decorator only affects subclasses derived using regular inheritance?

PEP 3119 -- Introducing Abstract Base Classes states that: The @abstractmethod only affects subclasses derived using regular inheritance; "virtual subclasses" registered with the register() method are not affected. I'm wondering why this is the…
Simón Ramírez Amaya
  • 2,436
  • 2
  • 14
  • 31
2
votes
0 answers

An abstract subclass of OrderedDict? python3

A subclass that subclasses ABC and OrderedDict does not act as a true abstract class: >>> from abc import ABC, abstractmethod >>> from collections import OrderedDict >>> class AbstractOrderedDict(OrderedDict, ABC): ... @abstractmethod ... …
Asker
  • 105
  • 8
2
votes
1 answer

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 noticed something a little curious this morning: the…
Rick
  • 43,029
  • 15
  • 76
  • 119
2
votes
1 answer

@abstractmethod works just fine without metaclass=ABCMeta when it shouldn't

Consider this code from abc import ABCMeta, abstractmethod class C(): @abstractmethod def my_abstract_method(self): print('foo') class D(C): pass x = C() y = D() Neither x nor y is allowed by mypy yielding me a test.py:13:…
Christoph
  • 26,519
  • 28
  • 95
  • 133
2
votes
1 answer

How to impose to set a property in class definition

I want to create an abstract class such that subclasses will raise an error when instanced if they don't implement an abstract property. An error should be also raised if the attribute is a method rather than a property. My attempt: from abc import…
MLguy
  • 1,776
  • 3
  • 15
  • 28
2
votes
1 answer

Understanding MappingView ABC

What is the difference between a MappingView container and a Sized container? Any examples on how to implement a MappingView container? I might be misunderstanding ABCs and the docs entirely, but a MappingView container is any container that…
Michael B
  • 269
  • 1
  • 3
  • 10
2
votes
0 answers

Python 2: create abstract base class inheriting from HTMLParser

I have been working with HTML parsers in my latest Python 2 project (2.7.12). I want to create a single base class from which all of my HTML parser subclasses can inherit. However, I'd also like this base class (which inherits from HTMLParser) to be…
caleb531
  • 4,111
  • 6
  • 31
  • 41