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

149
Vistas
Abstract classes with varying amounts of parameters

I was wondering if its possible when creating an abstract class with abstract methods if its possible to allow the implementations of those methods in the derived classes to have different amounts of parameters for each function.

I currently have for my abstract class

from abc import ABCMeta, abstractmethod

class View(metaclass=ABCMeta):
    @abstractmethod
    def set(self):
        pass

    @abstractmethod
    def get(self):
        pass

But I want to be able to implement it in one class with set having 1 parameter and get having 2 (set(param1) and get(param1, param2)), and then in another class also inherit it but have 0 parameters for set and 2 for get (set() and get(param1, param2)).

Is this possible and if so how would I go about doing it

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

0

No checks are done on how many arguments concrete implementations take. So there is nothing stopping your from doing this already.

Just define those methods to take whatever parameters you need to accept:

class View(metaclass=ABCMeta):
    @abstractmethod
    def set(self):
        pass

    @abstractmethod
    def get(self):
        pass


class ConcreteView1(View):
    def set(self, param1):
        # implemenation

    def get(self, param1, param2):
        # implemenation


class ConcreteView2(View):
    def set(self):
        # implemenation

    def get(self, param1, param2):
        # implemenation
over 4 years ago · Santiago Trujillo Denunciar

0

python 3.8

from abc import ABC, abstractmethod


class SomeAbstractClass(ABC):
    @abstractmethod
    def get(self, *args, **kwargs):
        """
        Returns smth
        """

    @abstractmethod
    def set(self, key, value):
        """
        Sets smth
        """

class Implementation(SomeAbstractClass):
    def set(self, key, value):
        pass

    def get(self, some_var, another_one):
        pass

Works perfectly, no warnings, no problems

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