Callable function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
12th Jun 2020, 11:23 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
33 Answers
+ 7
This link has some meaningful solution in python with some good approach have an look if this help Tomiwa Joseph https://www.sololearn.com/post/355059/?ref=app
13th Jun 2020, 9:57 AM
Preity
Preity - avatar
+ 5
Wow, XXX, nice! 😁 It would be great if we got the actual number in the end, so that you could write something like: n: int = add(1)(2)
13th Jun 2020, 8:42 AM
HonFu
HonFu - avatar
+ 4
I couldn't think of a solution better than HonFu 's answer. But if callable classes are okay, you can do something like this https://code.sololearn.com/ckKYGh7RH1RG/?ref=app
13th Jun 2020, 8:37 AM
XXX
XXX - avatar
+ 4
HonFu that would be nice. But I can't think of a solution to that. Any suggestions?? Edit: The only thing I can think of is using the 'with' statement (updated the code to reflect that).
13th Jun 2020, 9:51 AM
XXX
XXX - avatar
+ 4
XXX, unfortunately not! In my mind, the function would have to know in time, if it will be called again - which obviously isn't really possible. I'm sure there is a trick, but I don't see it.
13th Jun 2020, 9:54 AM
HonFu
HonFu - avatar
+ 4
XXX, hm, all of the Python solutions there have some sort of circumvention in them. Either they return a string representation like you showed (have you gotten that from the challenge thread?), or they have a show function that has to sum it all up.
13th Jun 2020, 10:12 AM
HonFu
HonFu - avatar
+ 4
HonFu the post mentioned by Preity has a really genius code by Tibor Santa, that is, to inherit from int. So in the __call__ method we will actually be returning a subtype of int which can be added to ints
13th Jun 2020, 12:54 PM
XXX
XXX - avatar
+ 4
Wow, so simple! 😶 That guy knows his way around the Py. 😁
13th Jun 2020, 3:08 PM
HonFu
HonFu - avatar
+ 4
HonFu Yes that's right. But let me know if this is possible? Or am I just disturbing my brain?
15th Jun 2020, 1:36 PM
Valmob101
Valmob101 - avatar
+ 3
Hm, tricky. I'm at a loss so far. 😅 This is the closest I've come: def add(call=None, n=[]): if call: n.append(call) return add return sum(n) But it only works with an empty call at the end: print(add(2)(3)(7)()) Something with closure... maybe... or... 🤔🤯
12th Jun 2020, 8:46 PM
HonFu
HonFu - avatar
+ 3
If I try to use the function a second time, I get an error.
15th Jun 2020, 6:40 AM
HonFu
HonFu - avatar
+ 3
Two possibilities: 1.) You make it. Then it was possible. 2.) You give up. Then you'll always wonder: Would it have been possible or not? The general task is solvable, the aforementioned solution, inheriting from int, just great. That doesn't necessarily mean there's no other way to do it.
15th Jun 2020, 1:42 PM
HonFu
HonFu - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 it was in the original code that the problem was in. Trying to pass a second argument outside of a function call. All i did was move a number back in to the call. Check OPs code
12th Jun 2020, 11:57 AM
Slick
Slick - avatar
12th Jun 2020, 7:55 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
Waoh, 😳😳 really close infact. Good job HonFu It is indeed tricky.
12th Jun 2020, 10:26 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
This is another cool way to go about it XXX waoh 😀. Thanks guys. There is just so much...too much to learn.
13th Jun 2020, 8:47 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
Really appreciate this Preity
13th Jun 2020, 11:18 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
Tricker that's not really a solution to the problem. What if you want to use the function in the interactive mode.
14th Jun 2020, 3:48 PM
XXX
XXX - avatar
+ 2
14th Jun 2020, 4:23 PM
Valmob101
Valmob101 - avatar
+ 2
HonFu sorry, I don't get you.
14th Jun 2020, 4:30 PM
Valmob101
Valmob101 - avatar