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
Primitive call #461
Merged
Merged
Primitive call #461
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov Report
@@ Coverage Diff @@
## master #461 +/- ##
==========================================
+ Coverage 96.46% 96.48% +0.02%
==========================================
Files 98 99 +1
Lines 8623 8675 +52
==========================================
+ Hits 8318 8370 +52
Misses 305 305
Continue to review full report at Codecov.
|
kmax12
previously requested changes
Mar 12, 2019
CharlesBradshaw
commented
Mar 13, 2019
CharlesBradshaw
commented
Mar 13, 2019
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
We don't want to call get_function in the init.
We could store the output of self.get_function() and check if that exists every time in the call function.
That goes against the pythonic "it's Easier to Ask for Forgiveness than Permission" (EAFP) in this case we're using "Look Before You Leap" (LBYL). To fix this we can use a try except instead.
We already know when we need to call
get_function
, and it's on the first time__call__
is run. So instead of using the try except function we can callget_function
in the__call__
function, and then redefine__call__
to be the output ofget_function
This way there are no unnecessary checksThis approach doesn't work because
__call__
is a special method. When a class instance, for exampleprimitive
gets called likeprimitive(data)
, python under the hood runstype(primitive).__call__(primitive,data)
instead ofprimitive.__call__data
We can get around this by using a non special method as an intermediate.
This way the second time
_temp_call
has been called, it's set to the output ofget_function
.Note that the
self.get_function()
has__get__(self)
also. This is because to add a function to an instance of a class, and not the class itself, you have to bind the instance to the function, aka addself
as the first parameter always. I don't fully understand how this works, but it's done using__get__(x)
Given that we can't overwrite call, and that it isn't standard to add/update the methods of an instance, it feels like the if statement, or the try except would be the better solution
Fixes #460