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

288
Vistas
How to type hint a generic numeric type in Python?

Forgive me if this question has been asked before but I could not find any related answer.

Consider a function that takes a numerical type as input parameter:

def foo(a):
    return ((a+1)*2)**4;

This works with integers, floats and complex numbers.

Is there a basic type so that I can do a type hinting (of a real existing type/base class), such as:

def foo(a: numeric):
    return ((a+1)*2)**4;

Furthermore I need to use this in a collection type parameter, such as:

from typing import Collection;
def foo(_in: Collection[numeric]):
    return ((_in[0]+_in[1])*2)**4;
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There isn't a generic numeric type in the typing module, so you would have to create such a type with Union instead:

from typing import Union

numeric = Union[int, float, complex]

...

To add support for Numpy's collection of numeric types, add np.number to that Union.

numeric = Union[int, float, complex, np.number]
over 4 years ago · Santiago Trujillo Denunciar

0

PEP 3141 added abstract base classes for numbers, so you could use:

from numbers import Number

def foo(a: Number) -> Number:
    ...
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