0

I am not sure if this is posible in Python: suppose I have a function such as

def is_even(x) :
    if type(x) == int :
        return mx % 2 == 0
    else :
        return NotImplemented

Suppose I want to extend it to a different class, say fractions, I would like to be able to redefine is_even for fractions without knowing for which other types it is defined, something like:

def is_even(x) :
    if type(x) == Fraction :
        return x.numerator % 2 == 0
    else :
        return the other is_even(x)

Again I want to define it for many types without knowing for which other types it might be already defined (if any).

Esteban Crespi
  • 315
  • 1
  • 8
  • 1
    `def is_even` would overwrite any previous `is_even` funcs or vars. You could put it into a class and use inheritance, or put a big conditional switching on type inside the one function. But this strikes me as an [x-y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you provide more context? – ggorlen Feb 19 '20 at 16:22
  • python doesn't support overloading because python doesn't know the data type before runtime. just check for type(your_parameter) and use if block – Atreyagaurav Feb 19 '20 at 16:42
  • @ggorlen Thanks It does. I had finally found a different Solutions but this one is better. – Esteban Crespi Feb 20 '20 at 22:43

0 Answers0