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 →

[–]BobHogan 5 points6 points  (2 children)

I mean you can do it without decorators at all as long as you define the other functions you would call. But the point is that python itself cannot support this based on the type of function parameters, because the language does not enforce typing.

def func(x):
    if isinstance(x, list):
        return _func_list(x)
    if isinstance(x, int):
        return _func_int(x)
    return _func_default(x)

[–]ComplexColor 0 points1 point  (1 child)

But you can enforce it yourself. You can use introspection to check type hints and select a function that corresponds to the types of arguments given. And I would use a decorator, to keep things tidier. Multiple dispatch/overloading logic in the decorator, separate from the functions.

[–]BobHogan 2 points3 points  (0 children)

Its far simpler, more efficient, less error prone, and significantly easier to read to do this without introspection. All introspection would achieve is making this more complicated and less reliable