Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Primitive call #461
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.
def __call__(self,data): if not hasattr(self,method): self.method = self.get_function() return self.method(data)
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.
def __call__(self,data): try: return self.method(data) except AttributeError: self.method = self.get_function() return self.method(data)
We already know when we need to call
def __call__(self,data): self.__call__ = self.get_function() return self.__call__(data)
This approach doesn't work because
We can get around this by using a non special method as an intermediate.
def __call__(self,*args,**kwargs): self._temp_call(*args,**kwargs) def _temp_call(self, *args, **kwargs): self._temp_call = self.get_function().__get__(self) return self._temp_call(*args,**kwargs)
This way the second time
Note that the
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
@@ 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