小编Anh*_*yen的帖子

如何替换python3中的has_key?

我尝试安装Auto-SelfControl并在执行此命令时卡住了:

sudo /usr/bin/python auto-selfcontrol.py
Run Code Online (Sandbox Code Playgroud)

它显示错误:

AttributeError:“dict”对象没有属性“has_key”

我正在寻找解决方案,最终将has_key替换为in运算符,但由于我只了解 python 的基础知识,所以代码对我来说相当复杂。

有 3 个地方使用了has_key,你能帮我更改它以便我可以使用 python3 运行吗?

1.

def check_if_running(username):
""" checks if self-control is already running. """
defaults = get_selfcontrol_settings(username)
return defaults.has_key("BlockStartedDate") and not NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])
Run Code Online (Sandbox Code Playgroud)

2-4.

def check_config(config):
""" checks whether the config file is correct """
if not config.has_key("username"):
    exit_with_error("No username specified in config.")
if config["username"] not in get_osx_usernames():
    exit_with_error(
            "Username '{username}' unknown.\nPlease use your OSX username instead.\n" \
            "If you have trouble …
Run Code Online (Sandbox Code Playgroud)

python macos python-3.x

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

macos ×1

python ×1

python-3.x ×1