abstract-class

How to detect method overloading in subclasses in python?

How to detect method overloading in subclasses in python? Question: I have a class that is a super-class to many other classes. I would like to know (in the __init__() of my super-class) if the subclass has overridden a specific method. I tried to accomplish this with a class method, but the results were wrong: …

Total answers: 10

Java abstract/interface design in Python

Java abstract/interface design in Python Question: I have a number of classes which all share the same methods, only with different implementations. In Java, it would make sense to have each of these classes implement an interface or extend an abstract class. Does Python have anything similar to this, or should I be taking an …

Total answers: 5

How to create abstract properties in python abstract classes

How to create abstract properties in python abstract classes? Question: In the following code, I create a base abstract class Base. I want all the classes that inherit from Base to provide the name property, so I made this property an @abstractmethod. Then I created a subclass of Base, called Base_1, which is meant to …

Total answers: 7

abstractmethod is not defined

abstractmethod is not defined Question: I cannot run this code, because I get the exception: NameError: name ‘abstractmethod’ is not defined File “C:TeststrunkPythonTestsAbstractClasses.py”, line 12, in <module> class MyIterable: File “C:TeststrunkPythonTestsAbstractClasses.py”, line 15, in MyIterable @abstractmethod from abc import ABCMeta class Foo(object): def __getitem__(self, index): print ‘__get_item__ Foo’ def __len__(self): print ‘__len__ Foo’ def get_iterator(self): …

Total answers: 3

`staticmethod` and `abc.abstractmethod`: Will it blend?

`staticmethod` and `abc.abstractmethod`: Will it blend? Question: In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this? I tried applying both decorators, but it doesn’t work. If I do this: import abc class C(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @staticmethod def my_function(): pass …

Total answers: 5

Why use Abstract Base Classes in Python?

Why use Abstract Base Classes in Python? Question: Because I am used to the old ways of duck typing in Python, I fail to understand the need for ABC (abstract base classes). The help is good on how to use them. I tried to read the rationale in the PEP, but it went over my …

Total answers: 6

Abstract attributes in Python

Abstract attributes in Python Question: What is the shortest / most elegant way to implement the following Scala code with an abstract attribute in Python? abstract class Controller { val path: String } A subclass of Controller is enforced to define “path” by the Scala compiler. A subclass would look like this: class MyController extends …

Total answers: 13

Equivalent of NotImplementedError for fields in Python

Equivalent of NotImplementedError for fields in Python Question: In Python 2.x when you want to mark a method as abstract, you can define it like so: class Base: def foo(self): raise NotImplementedError(“Subclasses should implement this!”) Then if you forget to override it, you get a nice reminder exception. Is there an equivalent way to mark …

Total answers: 8

Abstract class + mixin + multiple inheritance in python

Abstract class + mixin + multiple inheritance in python Question: So, I think the code probably explains what I’m trying to do better than I can in words, so here goes: import abc class foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def bar(self): pass class bar_for_foo_mixin(object): def bar(self): print “This should satisfy the abstract method requirement” class …

Total answers: 2

Difference between abstract class and interface in Python

Difference between abstract class and interface in Python Question: What is the difference between abstract class and interface in Python? Asked By: gizmo || Source Answers: Python doesn’t really have either concept. It uses duck typing, which removed the need for interfaces (at least for the computer :-)) Python <= 2.5: Base classes obviously exist, …

Total answers: 8