In a previous post, I talked about python partials. They provide a way to make a new function from another with some parameters at the front pre-filled. What can we do to fix this? Well there is a 3rd party library called funcy that provides an rpartial method. Or you could do something custom, like making a callable. You can make a callable class by implementing the magic function __call__. This lets you call an object instance like a method.
thing = MyCallableClass()
result = thing()
So now let's apply this concept to implement rpartial.
class RPartial:
def __init__(self, method, *args):
self.method = method
self.args = args
def __call__(self, *args):
return self.method(*(args + self.args))
import os
r_join = RPartial(os.path.join, "my", "cool", "path")
print(r_join("C:\\")) # c:\my\cool\path is output
This is much more verbose than funcy's version of rpartial, but it does serve well as an example of how you can use callables. You could definitely make something more specific for your needs.
Comments !