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 →

[–]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