我在Python中看到过几种不同的写作文档字符样式,是官方还是"同意"的风格?
我把空括号作为Python函数的可选参数的默认值,而pylint(使用Sublime包)告诉我这很危险.有人可以解释为什么会这样吗?而使用None是更好的选择吗?
试图找到一种方法来清理我的一些代码.
所以,我的Python代码中有这样的东西:
company = None
country = None
person = Person.find(id=12345)
if person is not None: # found
company = Company.find(person.companyId)
if company is not None:
country = Country.find(company.countryId)
return (person, company, country)
Run Code Online (Sandbox Code Playgroud)
在阅读了关于Haskell的monad(特别是Maybe)的教程后,我想知道是否可以用另一种方式编写它.
据我所知,以下两种类型在 Python 中是等效的:
Optional[Union[A, B]]
Union[A, B, None]
是否有一个明确的约定可供选择,例如 PEP 中的条款?
我试图了解如何使用 Python 3.9 类型提示来注释接收具有可选或多个类型值的字典的函数。我知道通过打字模块,我可以轻松使用Union和Optional. 但我想知道是否可以仅使用 Python 3.9 注释来实现这一点。我在PEP 585上没有找到任何关于此事的信息。
例子:
From Typing import Dict, Optional, Union
def foo(bar: Dict[Union[str, int], Optional[str]]) -> bool: return True
Run Code Online (Sandbox Code Playgroud)
我应该这样做吗:
from __future__ import annotations
def foo(bar: dict[[str, int], [str, None]) -> bool: return True
Run Code Online (Sandbox Code Playgroud) python ×5
type-hinting ×2
coding-style ×1
docstring ×1
haskell ×1
mypy ×1
pylint ×1
python-3.x ×1
union-types ×1