+ 1

python func

alright, i can't get my head around it can someone explain the use of the first def and the "arg" in detail.... def apply_twice(func, arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five, 10))

31st Jul 2021, 5:20 PM
Tomoe
Tomoe - avatar
6 Answers
+ 7
Even if you use another name instead of arg and func, it will work as same. def is used to define function. First in the line, print(apply_twice(add_five,10)) You are calling the apply_twice function where you are giving add_five function passed inside the first parameter and the 10 in the second parameter. Next in the apply_twice function, you are returning func(func(arg)) here the func is refered to the add_five and arg is refered to 10. So in short, you are returning return add_five(add_five(10)) Here .First add_five(10) will return 10+5 which is 15.Now if you simplify the previous line, it becomes: return add_five(15) now it returns 15+5=20 So it returns 20 and prints 20 Summery, return func(func(arg)) =>return add_five(add_five(10)) =>return add_five(15) =>return 20 So it prints 20.
31st Jul 2021, 5:31 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 4
Tomoe Explanation is here. In your case output is 20 because of 2 arguments in function. https://code.sololearn.com/cgfonDJu8pH0/?ref=app
31st Jul 2021, 6:08 PM
A͢J
A͢J - avatar
+ 1
Tomoe All you're doing is to pass a function reference as the first parameter to the apply_twice() function. Google about Python pass-by-reference to know more about this.
31st Jul 2021, 8:09 PM
Calvin Thomas
Calvin Thomas - avatar
0
def is a pre defined and we using it which declearing or defining function in python arg is a argument which u passing in rough way u can say it a passing function variable u van give any any name instead of args but u cannot change def with any bame gere u defined two functions first one is apply_twice and second one is afd_five(x) in both function u passing one parameter in print statement u calling add five function and passing value 10 so 10 will pass to x and return statement will execute and it will return 10+5 which is 15 same will be apply for next function becz in print statement u calling two functions so first time add will call then
31st Jul 2021, 5:27 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Akshat WhiteHat Junior is a scam business!
2nd Aug 2021, 8:07 AM
Calvin Thomas
Calvin Thomas - avatar
0
Can you help me ...i dont know how to write pythone code to count aspecfic name in text in file and how much time it is take to finish
2nd Aug 2021, 3:58 PM
Rana Saleh