You can make the objects themselves be callable by implementing __call__
(rename your get_self
)
def point(a, b)
return Point(a, b) # a Point is callable
except that now Point and point behave the same, so it should just be the class.
You might run into the sillyness that the return value when you call it is completely ignored, this is why you might want to use functions instead, and to instead send the information through the return value.
I solved it by letting my function accept a third value indicating whether the first or second value should be returned:
def point(a, b, msg):
if msg == 'fst':
return a
if msg == 'snd':
return b
Of course, that leaves out a whole lot of magic allowing it to be called multiple times until it has received 3 arguments:
point()()()()()(3)()()()(4)()()()()()()('fst') # 3
point()()()()()(3)()()()(4)()()()()()()('snd') # 4
point(3, 4)('fst') # 3
point(3, 4, 'fst') # 3
p = point(3, 4)
p('fst') # 3
p('snd') # 4
# trying to print this intermediary value doesn't make much sense.
point(3, 4) # functools.partial(<function curry.<locals>.deco.<locals>.wrapper at 0x7f1744d7a2f0>, 3, 4)
To that end I’ve got another function which keeps creating and returning new functions each time it is called, until all arguments have been received, at which point the answer is returned instead of another function. (Yes, it is a decorator)
def curry(...)
...
@curry
def point(a, b, msg):
if msg == 'fst':
return a
if msg == 'snd':
return b