. Advertisement .
..3..
. Advertisement .
..4..
This is the program I run:
with ParamExample(URI) as pe:
with MotionCommander(pe, default_height=0.3)as mc:
After I run, it returns an error:
Traceback (most recent call last):
File "test44.py", line 156, in <module>
with ParamExample(URI) as pe:
AttributeError: __enter__
Does anyone have any suggestions for the problem below: attributeerror: __enter__ in the python- How to correct it?
The cause: This error happens because you don’t have the __enter__ method on your class.
Solution: You can resolve this error by two ways. The first way is you need indent the second with block so that it’s actually in the first. The second way is substituting these lines with:
It is similar with interlocking these two context managers (blocks need to used inthe name of the objects).
Additional code would be greatly appreciated, especially the
ParamExample
implementation. However, I assume you are missing__enter__
(and possibly__exit__
) on this class.If you use the
with
block of python the object in with statement gets its__enter__
method called. The block within thewith
then runs and the__exit__
is called (optionally with exception information if one was raised). This error will occur if your class doesn’t have a__enter__
.Side note: You can either indent
with
block 2 so it is actually within the first or replace these two lines byThis is equivalent to nesting these context managers (the names of objects used by
with
blocs).