Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

underscore lambda cannot use __contains__ (IOW "in") [renamed issue] #65

Open
mjkaye opened this issue Aug 29, 2014 · 2 comments
Open

Comments

@mjkaye
Copy link
Contributor

mjkaye commented Aug 29, 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:

    my_filter = F() >> (filter, _[1] in TYPE_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

@Digenis
Copy link
Contributor

Digenis commented Sep 1, 2014

__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

Look at this:

from operator import contains
from fn import F, _

my_filter = F(filter, F(contains, TYPE_MAP) << _[1])

@mjkaye 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
@mjkaye
Copy link
Contributor Author

mjkaye commented Sep 3, 2014

Thanks Digenis. I should have done more testing to find the real reason for the error. I've renamed the issue to reflect the underlying restriction.

Perhaps the documentation could be updated to more clearly explain the limitations of underscore lambdas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants