Search code examples
pythonpython-3.xboto3

overloading with multipledispatch when working in boto3


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?


Solution

  • 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?