2
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.

chattershuts
  • 366
  • 2
  • 10

1 Answers1

3

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
parsariyahi
  • 61
  • 1
  • 6
  • 1
    there's truth in this, but you're missing a crucial point - what does the `__init__` method look like, since you'll need one? One option is to use something like in an [answer of mine](https://stackoverflow.com/a/68366676/5986907) – joel Sep 06 '22 at 23:03
  • Agree, I like the solution @joel proposed better. Would you mind reporting it as answer to this question? I might do it myself, but it's yours, I don't want to steal it. – chattershuts Sep 12 '22 at 08:59
  • @chattershuts you're welcome to copy-paste if you want, but might this be a duplicate question if you want to do that? – joel Sep 12 '22 at 09:42