具有一些强制键作为功能输入的字典

发布于 2021-01-29 15:00:55

我有一个以字典为参数的函数。我将传递给各种字典的字典,这些字典比函数内部使用的字典要多。另外,我想在函数定义中查看需要哪些键。所以我写

def fun(indict=dict(apple=None, pear=None)):

但是,该函数现在接受任何输入为indict。有没有聪明的写作方法

any dictionary that has at least the keys 'apple' and 'pear' is accepted.

就像是

def fun(indict=dict(apple=NeedsToBeSpecified, pear=NeedsToBeSpecified)):
关注者
0
被浏览
93
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    在python3.x中,您可以使用函数注释

    >>> def foo(indict: dict(apple=None, pear=None)):
    ...     print(indict)
    ... 
    >>> foo(dict())
    {}
    

    您甚至可以疯狂地使用现在被口译人员接受的Ellipsis文字

    >>> def foo(indict: dict(apple=None, pear=None, extra_items=...)) -> int:
    ...     if any(x not in indict for x in ('apple', 'pear')):
    ...         raise ValueError('message here...')
    ...     print(indict)
    ...     return 3
    ... 
    >>> foo({})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in foo
    ValueError: message here...
    >>> foo({'apple':6, 'pear':4})
    {'pear': 4, 'apple': 6}
    3
    >>> foo({'apple':6, 'pear':4, 'carrot':30000})
    {'carrot': 30000, 'pear': 4, 'apple': 6}
    3
    

    你可以从我的第一个例子中看到,注释不 执行
    任何东西。您必须在函数本身中执行验证,尽管我想您可以从注解1中检查所需的键,如果您想使其保持DRY,但是仅使用2个键可能就不值得了……

    在python2.x中(以及更传统的情况下),也许您只想将信息放入文档字符串中;-)-我也建议您对python3.x进行此操作,因为这是查找的传统位置用于文档…

    1个keys = foo.__annotations__['indict'].keys() - {'extra_items'}

    更新
    请注意,现在像mypy这样的奇特事物闲坐着,这个答案可能有些过时了。您可以考虑使用TypedDictfrom进行注释mypy_extensions。如果您使用类似类型的检查器,那应该为您的用户设定期望,甚至可能帮助捕获一些错误mypy

    from mypy_extensions import TypedDict
    
    class Apple:
        """Represent an Apple."""
    
    class Pear:
        """Represent a Pear."""
    
    # "annotation-type" for a dictionary that has an apple and pear key whose values are Apple and Pear instances.
    FruitBowl = TypedDict("FruitBowl": {"apple": Apple, "Pear": Pear})
    
    def foo(indict: FruitBowl) -> int:
        ...
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看