Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

330
Vistas
Python3 - How does one define an abstract subclass from an existing abstract class?

I initially defined the following abstract class:

from abc import ABC, abstractmethod    
class Primitive(ABC):

Now I want to create another abstract class that inherits from Primitive:

class InstrumentName(Primitive)

I need this class to be abstract since I ultimately want to create the following two concrete classes:

class CurrencyInstrumentName(InstrumentName)
class MetalInstrumentName(InstrumentName)

I have read the documentation and searched SO, but they mostly pertain to sublcassing concrete classes from abstract classes, or discussing how Python handles abstraction

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Just subclass, you don't need to do anything special.

A class only becomes concrete when there are no more abstractmethod and abstractproperty objects left in the implementation.

Let's illustrate this:

from abc import ABC, abstractmethod    
class Primitive(ABC):
    @abstractmethod
    def foo(self):
        pass

    @abstractmethod
    def bar(self):
        pass

class InstrumentName(Primitive):
    def foo(self):
        return 'Foo implementation'

Here, InstrumentName is still abstract, because bar is left as an abstractmethod. You can't create an instance of that subclass:

>>> InstrumentName()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class InstrumentName with abstract methods bar

Subclasses can also add @abstractmethod or @abstractproperty methods as needed.

Under the hood, all subclasses inherit the ABCMeta metaclass that enforces this, and it simply checks if there are any @abstractmethod or @abstractproperty attributes left on the class.

over 4 years ago · Santiago Trujillo Denunciar

0

As @MartijnPieters wrote, you don't need to do anything special for Python, but PyCharm will warn:

Class InstrumentName must implement all abstract methods

One way to suppress that warning:

import abc

class Primitive(abc.ABC):
    @abc.abstractmethod
    def foo(self):
        pass

# noinspection PyAbstractClass
class InstrumentName(Primitive):
    def is_tuba(self):
        return False

Another way:

...

class InstrumentName(Primitive, metaclass=abc.ABCMeta):
    def is_tuba(self):
        return False
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda