1

I would like to have two functions same name(overloaded) that use these boto3 functions. The function bodies would be different since roles have tags but groups do not.

get_role(RoleName = rolename)
get_group(GroupName = groupname)

How exactly would I accomplish this, is it possible or am I thinking about it incorrectly?

2
  • Why use the same name if one function gets a role and the other gets a group?
    – DeepSpace
    Commented Jul 5 at 16:53
  • Because in this specific use case the user may not know the difference and I want to be able to handle both cases
    – Tristan
    Commented Jul 5 at 17:09

1 Answer 1

0

I don’t think multipledispatch is your way out, here. If the call signature requires one thing, descended from two possible base classes, some if/then logic (or hey, maybe even the match/case new hotness) examining type(thing) or somesuch will get you through.

Another solution, one more explicit than the above, is to use named parameters. Using your snippet, it’d look something like this:

def get(*, role=None, group=None):
    """ Herein you’d use logic based on
        the named parameters to execute
        your logic accordingly
    """
    ...

… this would also force your API users to think about what they are passing you, which is a better deal for everyone, versus trying to compensate for whatever they might do, which is fundamentally unknowable, right?

1
  • Personally, also, I hate multipledispatch as I am a huge C++ fan. If you are at all curious, I wrote my own type-based polymorphic dispatch thing, based on a snippet from PyPy. It uses annotations or explicit types in its decorator syntax – the library and example test code are here: github.com/fish2000/CLU/blob/master/clu/extending.py
    – fish2000
    Commented Jul 5 at 18:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.