[Solved] Challenge Question from Etabeta1: test()()()() | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

[Solved] Challenge Question from Etabeta1: test()()()()

"How many times will this code output "x"? def test(): print("x") return test test()()()()" I am having a hard time understanding the syntax of this recursive function at the function calling part. So it calls the function 4 times because what it returns is the function without the parantheses? Why doesn't the interpreter interpret the content of the outermost pair of parantheses as argument and then throw an argument exception or something because the function definition has no parameter asking to be passed an argument? And does the interpreter interpret stuff from left to right and not in blocks or smallest unit of inseparable entities? Lol, sorry for babbling, I just don't know how to ask this. I stared at this for a while, like a chimp who's just witnessed thunderbolt the first time or something. Scratching my head.

3rd Apr 2022, 12:47 AM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
5 Respostas
+ 3
Don't be fooled into thinking that this is a recursion example. The function fully returns before the next call begins. def test(): print("x") return test test()() #the above gets executed the same as: temp=test() temp() #Next expansion: test()()() #gets executed the same as: temp=test() temp1=temp() temp1() #the pattern continues: test()()()() #gets executed the same as: temp=test() temp1=temp() temp2=temp1() temp2()
3rd Apr 2022, 1:39 AM
Brian
Brian - avatar
+ 3
Jay Matthews Thank you for the code. If I may: 1) Is the () itself an operator that can stand alone or something. Gah I really don't know how to ask this. It feels to me like writing 4+ and returning to keep adding 4's, as if + could hang in the air. or as if () were a string that becomes alive as a function container when func name precedes it. Connecting the function object with the container like that... 2. I am guessing copying and pasting your code into my notes wouldn't pose a problem?
3rd Apr 2022, 1:16 AM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
+ 2
Jay Matthews OK, I will. I didn't know I was allowed to play with private files, thank you, (I need to revisit Yeva's post, I am not always confident in what I do at the site)
3rd Apr 2022, 1:48 AM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
+ 2
KorkunƧ TheTerrible thanks for posting the question. The code helped me see, too, that there is another way to perform iteration that I hadn't considered before. The trick should come in handy for challenges that require looping without using either for or while statements.
4th Apr 2022, 12:24 AM
Brian
Brian - avatar
+ 1
I congratulate Etabeta1 for asking such a question. It is fundemental to understanding how the interpreter works, or the function as an object, and I am extra grateful for the help offered here. For beginners like me: This is not a recursive function as Brian let me realize(thanks for the extra help) , plus, there wouldn't have been 4 successive calls if it were, right. ID'ing the function object and return value also shows how these are the same thing, id() seems a nice tool for debugging. Many thanks for the kindness and the help :-)
3rd Apr 2022, 9:59 PM
KorkunƧ el Gato
KorkunƧ el Gato - avatar