我正在从PHP转换到Python。在PHP中,isset()和empty()可以很容易地帮助您确定是设置了数组键(Python中的dictionary键)还是为空(false)。你知道吗
在Python3中,有没有一种简单的方法可以查看字典键是设置的还是空的?我注意到一些解决方案,这些解决方案指向各种尝试/例外捕获。你知道吗
PHP isset()函数
https://www.php.net/manual/en/function.isset.php
Determine if a variable is considered set, this means if a variable is declared and is different than NULL.
PHP empty()函数
https://www.php.net/manual/en/function.empty.php
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.
您可以使用
in
测试字典中是否存在键:如果get中不存在键,还可以尝试查找值并获取默认值(默认情况下
None
):相关问题 更多 >
编程相关推荐