[Twisted-Python] Deferred instance has no attribute '__getitem__' ??????
glyph at twistedmatrix.com
Thu Jul 15 23:38:44 EDT 2010
On Jul 15, 2010, at 4:32 PM, Ken MacDonald wrote:
> def round_val(id, value, rule):
> Return first element of round_value_and_percent
> defer.returnValue(round_value_and_percent(id, value, 0, rule))
> When I do:
> result = yield round_val(1, 2, 3)
> I get this: Deferred instance has no attribute '__getitem__'
> Clues appreciated!
@inlineCallbacks must decorate only generator functions.
Since 'yield' doesn't appear anywhere in the body of round_val (or, for that matter, round_per) this code will break in other ways. Allen Short's suggestion will fix your code because it added a 'yield', but you need to be aware of this issue in case there are other such functions in the future that don't actually handle any Deferreds.
Also, inlineCallbacks adds some overhead, and really should only be used when the resulting code is easier to read. Personally, I think a simpler definition of round_val would be simply
def round_val(id, value, rule):
Return first element of round_value_and_percent
return round_value_and_percent(id, value, 0, rule).addCallback(
lambda valueAndPercent: valueAndPercent
no @inlineCallbacks required.
More information about the Twisted-Python