gpt4 book ai didi

python - 找出在某些条件下返回空哈希的方法的返回值

转载 作者:行者123 更新时间:2023-12-03 07:54:31 25 4
gpt4 key购买 nike

我正在尝试了解如何进行这项工作:

def someMethod() -> dict[any, any]:
if not os.path.exists('some path'):
return {}

config = {'a': 1, 'b': 2}
return config

我认为这是不正确的。看到此错误 - 声明的返回类型“dict[Unknown, Unknown]”部分未知Pylance

这个想法是,如果路径不存在(或在某些条件下),则返回空字典,或者使用键值对纠正字典。

有什么想法吗?

最佳答案

小写any是Python内置函数而不是类型。相反,您必须从输入模块导入大写字母 Any

from typing import Any
import os

def someMethod() -> dict[Any, Any]:
if not os.path.exists('some path'):
return {}

config = {'a': 1, 'b': 2}
return config

关于python - 找出在某些条件下返回空哈希的方法的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76358367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com