Skip to content
Advertisement

Tag: abstract-class

Abstract base class using type of subclass in abstractmethods

I want to create an abstract base class with an abstract method that returns an instance of any subclass that implements the interface. Is there a more pythonic way to achieve this for mypy other than what I have created below? In my code example, I make the Animal class generic. Subclasses can inherit from Animal and specify the generic

What’s the difference between an inner class and an inner inner class in python? [duplicate]

This question already has answers here: Short description of the scoping rules? (9 answers) Closed 7 months ago. Shouldn’t field be undefined on line 50? It was my understanding that inner nested classes did not have visibility to outer classes, as I ran into on line 65… Just seems kind of inconsistent and I would love to have a better

Python abstract classes – how to discourage instantiation?

I come from a C# background where the language has some built in “protect the developer” features. I understand that Python takes the “we’re all adults here” approach and puts responsibility on the developer to code thoughtfully and carefully. That said, Python suggests conventions like a leading underscore for private instance variables. My question is, is there a particular convention

Equivalent of NotImplementedError for fields in Python

In Python 2.x when you want to mark a method as abstract, you can define it like so: Then if you forget to override it, you get a nice reminder exception. Is there an equivalent way to mark a field as abstract? Or is stating it in the class docstring all you can do? At first I thought I could

Advertisement