繁体   English   中英

暗示 python function 接受两个 arguments 但只能填充其中一个的最干净的方法是什么?

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

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

编写 Authenticator 的最简洁方法是什么?可以实例化或通过 api 密钥或通过用户名密码元组(但显然不是两者)? 我想将两个参数都保留在构造函数中。

我认为如果您使用两种不同的方法而不是构造方法,它会更具可读性

class Authenticator:

    def auth_by_key(api_key):
        pass

    def auth_by_u_p(u, p):
        pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM