Python 错误 : AttributeError: __enter__

标签 python python-3.x crazyflie

<分区>

我在尝试运行代码时收到属性错误。

    with ParamExample(URI) as pe:
    with MotionCommander(pe, default_height=0.3)as mc:

这是错误发生的地方。

Traceback (most recent call last):
File "test44.py", line 156, in <module>
with ParamExample(URI) as pe:
AttributeError: __enter__

这是我在终端中收到的回溯。 如果您需要查看我的更多代码,请告诉我。 感谢您的帮助,谢谢!

最佳答案

更多代码将不胜感激(特别是 ParamExample 实现),但我假设您缺少 __enter__(可能还有 __exit__) 该类的方法。

当你在 python 中使用 with block 时,with 语句中的对象会调用其 __enter__ 方法,with 中的 block 运行,然后 __exit__ 被调用(如果引发异常信息,则可选)。因此,如果您没有在您的类中定义 __enter__,您将看到此错误。

旁注:您需要缩进第二个 with block 以便它实际上位于第一个 block 内,或者将这两行替换为

with ParamExample(URI) as pe, MotionCommander(pe, default_height=0.3) as mc:

这与嵌套这两个上下文管理器(with block 使用的对象的名称)是一样的。

关于Python 错误 : AttributeError: __enter__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51427729/

相关文章:

python - 如何重用逻辑来处理 Python 的 tkinter GUI 中的按键和按钮单击?

python - 使用 seaborn/matplotlib boxplot 时的刻度频率

python-3.x - tensorflow 中的 'tf.contrib.rnn.DropoutWrapper' ' 到底是做什么的? (三个关键问题)

python - 线性搜索给定数组以给出所需的元素索引

python - 如何执行柯尔莫哥洛夫-斯米尔诺夫检验来测试 Gamma 函数?

python - 导入错误 : No module named 'rospy'

python - 在 Windows 上的 os.system 中转义双引号

python - 无法在 Ubuntu 中使用 yum 安装 python-pip ..已解决