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

Method calling from _ not working? #79

Open
jni opened this issue Jan 29, 2016 · 3 comments
Open

Method calling from _ not working? #79

jni opened this issue Jan 29, 2016 · 3 comments

Comments

@jni
Copy link

jni commented Jan 29, 2016

Hi,

I'm not sure whether this is supposed to work:

from fn import _ as X
X.endswith('tif')('image.tif')

This prints "False".

Looking at the tests I see this form:

from fn import _ as X
X.call('endswith', '.tif')('image.tif')

This correctly evaluates to True, but is significantly uglier. What's happening in the first form?

@Digenis
Copy link
Contributor

Digenis commented Jan 30, 2016

The author of the module chose to treat .call() as a special attribute
because the usefulness of being able to create method callers
outweighs the usefulness of being able to use this very specific attribute getter.

You can subclass underscore to redefine .call() as an attribute getter.
You can also subclass underscore to turn attribute getters into method callers.
In no way can this be perfect because this is a hack built on top of python's data model.
This is a "can't fix" issue.

@jni
Copy link
Author

jni commented Jan 30, 2016

@Digenis I suspected it would not be fixable, but can you explain to me what is happening in the first form? I would expect it to either fail catastrophically or do the right thing.

@Digenis
Copy link
Contributor

Digenis commented Jan 31, 2016

_ is an instance of the underscore class.
Retrieving any attribute results in an attribute getter for such an attribute.
Calling it invokes this attribute getter with the 'tif' string as its first argument.
The endswith attribute of str however is a callable method.
You then call it on another string. tif doesn't end in image.tif so the result is false.
What happens is attrgetter('endswith')('tif')('image.tif') which is more like 'tif'.endswith('image.tif')

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