Python错误:AttributeError:__enter__ [英] Python Error: AttributeError: __enter__
问题描述
当我尝试运行代码时收到属性错误.
I receive the attribute error when I try to run the code.
with ParamExample(URI) as pe:
with MotionCommander(pe, default_height=0.3)as mc:
这是发生错误的地方.
This is where the error occurs.
Traceback (most recent call last):
File "test44.py", line 156, in <module>
with ParamExample(URI) as pe:
AttributeError: __enter__
那是我在终端中收到的回溯. 如果您需要查看更多我的代码,请告诉我. 任何帮助表示赞赏,谢谢!
That is the traceback that I receive in my terminal. If you need to see more of my code, please let me know. Any help is appreciated, thank you!
推荐答案
更多代码(特别是ParamExample
实现)将不胜感激,但是我假设您缺少__enter__
(可能还有__exit__
)在该类上的方法.
More code would be appreciated (specifically the ParamExample
implementation), but I'm assuming you're missing the __enter__
(and probably __exit__
) method on that class.
在python中使用with
块时,with语句中的对象将调用其__enter__
方法,运行with
中的块,然后调用__exit__
(可选地提供异常信息)如果有人提出来的话).因此,如果您在课程上没有定义__enter__
,则会看到此错误.
When you use a with
block in python, the object in the with statement gets its __enter__
method called, the block inside the with
runs, and then the __exit__
gets called (optionally with exception info if one was raised). Thus, if you don't have an __enter__
defined on your class, you'll see this error.
侧面说明:您需要缩进第二个with
块,使其实际上位于第一个with
块中,或者用
Side note: you need to either indent the second with
block so it's actually inside the first, OR replace these two lines with
with ParamExample(URI) as pe, MotionCommander(pe, default_height=0.3) as mc:
与嵌套这两个上下文管理器(with
块使用的对象的名称)相同.
which is the same as nesting these two context managers (the name of the objects used by with
blocks).
这篇关于Python错误:AttributeError:__enter__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!