我在尝试运行代码时收到属性错误。
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 使用的对象的名称)是一样的。