I am not a python guy and I am trying to understand some python code. I wonder what the last line of below code does? Is that kind of multiple objects returned? or list of 3 objects returned?
req = SomeRequestBean()
req.setXXX(xxx)
req.YYY = int(yyy)
device,resp,fault = yield req #<----- What does this mean ?
There are two things going on in that line. The easier one to explain is that the
yieldstatement is returning a value which is a sequence, so the commas take values of the sequence and put them in the variables, much like this:Now, the
yieldstatement is used to create a generator, which can return a number of values rather than just one, returning one value each timeyieldis used. For example:In effect, the function stops at the
yieldstatement, waiting to be asked for the next value before carrying on.In the example above
next()gets the next value from the generator. However, if we usesend()instead we can send values back to the generator which are returned by theyieldstatement back in to the function:Putting this all together we get:
A generator used in this way is called a coroutine.