What's the cleanest possible way to hint that a python function accepts two arguments but only one of them must be populated?

Viewed 41
class Authenticator:
    def __init__(api_key: str, user_pass: tuple[str, str]):
        raise NotImplemented

What' the cleanest way to write that Authenticator can be instantiated or via an api key or via a username-password tuple (but not both obviously)? I'd like to keep both parameters in the constructor.

1 Answers

I think it will be more readable if you use two different methods instead of constructor method

class Authenticator:

    def auth_by_key(api_key):
        pass

    def auth_by_u_p(u, p):
        pass
Related