如果我有这样的功能:添加默认参数的值与类型提示在Python
def foo(name, opts={}):
pass
我想类型提示添加到参数,我该怎么办呢?我假设的方式给了我一个语法错误:
def foo(name: str, opts={}: dict) -> str:
pass
下不会抛出一个语法错误,但它似乎并不像直观的方式来处理这种情况:
def foo(name: str, opts: dict={}) -> str:
pass
我可以在typing
documentation或Google搜索中找不到任何内容。
编辑:我不知道如何在Python中使用默认参数,但为了解决这个问题,我会保留上面的例子。一般来说,做以下事情要好得多:
def foo(name: str, opts: dict=None) -> str:
if not opts:
opts={}
pass
最后一个功能是正确的方法。 “scala”语言也是这样。 –
你有一个可变的默认类型 - 这将导致问题 –
看到我的更新答案,@josh –