You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often find myself in the situation where I want to create a filter and, if using the underscore syntax, _ is a sequence. For instance:
my_filter=F() >> (filter, _[1] inTYPE_MAP)
In this situation, _ will be an item from a dict.items() and will, therefore, be a 2-tuple. I want my_filter to return only the items for which the value (the second item in the 2-tuple) is in TYPE_MAP.
However, when run, the above code results in TypeError: "'bool' object is not callable". It looks as if it's trying to evaluate _[1] in TYPE_MAP at the time of partial function creation, rather than when actually calling the resulting function.
Is there a way to achieve this with the underscore syntax, or do I just have to use the lambda syntax?
Thanks
The text was updated successfully, but these errors were encountered:
__contains__() results are implicitly cast to bool, it can not return a callable to implement this trick.
Therefore underscore can not define this method to do what we would want it to mean here.
Follow this discussion https://mail.python.org/pipermail/python-ideas/2010-July/007733.html
mjkaye
changed the title
If _ is a sequence, I can't get to the elements
underscore lambda cannot use __contains__ (IOW "in") [renamed issue]
Sep 3, 2014
I often find myself in the situation where I want to create a filter and, if using the underscore syntax,
_
is a sequence. For instance:In this situation,
_
will be an item from adict.items()
and will, therefore, be a 2-tuple. I wantmy_filter
to return only the items for which the value (the second item in the 2-tuple) is inTYPE_MAP
.However, when run, the above code results in
TypeError: "'bool' object is not callable"
. It looks as if it's trying to evaluate_[1] in TYPE_MAP
at the time of partial function creation, rather than when actually calling the resulting function.Is there a way to achieve this with the underscore syntax, or do I just have to use the
lambda
syntax?Thanks
The text was updated successfully, but these errors were encountered: