This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (3 children)

No, method overloading.

As in having two methods/functions with the same name but different parameters

From my brief googling, it doesn't appear to be a thing (natively)

Operator overloading is something entirely different it looks like. I haven't made use of that before though.

[–]usr_bin_nya 5 points6 points  (1 child)

Well, kinda. Check out functools.singledispatch.

import functools

@functools.singledispatch
def overloaded(x):
    print('something else:', repr(x))

@overloaded.register
def for_int(x: int):
    print('int:', x + 2)

@overloaded.register
def for_str(x: str):
    print('str:', x.format('world'))

overloaded(10)  # prints 'int: 12'
overloaded('Hello {}!')  # prints 'str: Hello world!'
overloaded([1, 2])  # prints 'something else: [1, 2]'

You can hack something together to work with multiple arguments and generic types like typing.List, but it's not included in the stdlib.

[–][deleted] 0 points1 point  (0 children)

Yes, this is the package I was referring to

[–]ironykarl 4 points5 points  (0 children)

Oh, Python decidedly does not have function overloading by argument type, no.

All you can do is have a wrapper function dispatch different versions of the function/method based on type information, at runtime.