Difference between some functions and methods. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

Difference between some functions and methods.

There are some functions/methods which do the the same work as other ones do. like, variable.sort() and sorted(variable) again, >>> 3**3 and from math import pow print(pow(3,3)) Do they have some advantages over anothers?? Please inform if you know about some more pairs which do the same work.

8th Apr 2020, 8:25 AM
M Tamim
M Tamim - avatar
9 Answers
+ 6
import timeit from math import pow start_time=timeit.default_timer() print(3**3) print(timeit.default_timer()-start_time) start_time=timeit.default_timer() print(int(pow(3,3))) print(timeit.default_timer()-start_time) a=[1,2,4,2] start_time=timeit.default_timer() print(sorted(a)) print(timeit.default_timer()-start_time) a=[1,2,4,2] start_time=timeit.default_timer() a.sort() print(a) print(timeit.default_timer()-start_time)
9th Apr 2020, 2:36 PM
Abhay
Abhay - avatar
+ 9
Aymane Boukrouh [EXAMS] I did not ask for the diff. between 'function' and method. I asked for the diff. between relatively same fun. and methodes which outputs almost same. like the second example they output same all the time.
8th Apr 2020, 8:57 AM
M Tamim
M Tamim - avatar
+ 7
OLABAMIDELE FEMI please post your question as a que. in the discussion feed with the code you written(your trial)
9th Apr 2020, 10:45 AM
M Tamim
M Tamim - avatar
+ 6
There are multiple functions that do the same work, and the advantages vary depending in the project you want to make. However, for small codes, it shouldn't matter, because after all it's just different names for the same thing. Note that variable.sort() and sorted(variable) are NOT the same thing. variable.sort() changes the variable itself, while sorted(variable) only return a sorted version of it, but doesn't affect the variable. EDIT: I just noticed you were asking for functions/methods and not different functions with same output. Your second example is confusing :/ This question has been asked many times before, please use the searchbar before asking next time please.
8th Apr 2020, 8:43 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Yes, but if knowing the fundamental difference between the two types will justify the use of each one of them. Both of the examples you gave are not really related to your question, because in the first example the function and method are different, and in the second one there is no clear usage of method, so you might want to give better examples. However if they output the same result, then the only difference would be speed, and as I mentioned earlier speed is not very important for small codes. There is no global definition to which is faster, so google is your friend if you want to know which one fits better.
8th Apr 2020, 9:01 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Paste the code and you will see that 3**3 and sort takes less time than the other
9th Apr 2020, 2:37 PM
Abhay
Abhay - avatar
+ 2
Not sure if this is any help..but paste it into your ide: import timeit print(timeit.timeit(stmt="mylist.sort()", setup="mylist = [10, 3, 5, 4, 7, 9, 8, 6, 2, 1]")) print(timeit.timeit(stmt="list2 = sorted(mylist)", setup="mylist = [10, 3, 5, 4, 7, 9, 8, 6, 2, 1]")) print(timeit.timeit(stmt="3**3")) print(timeit.timeit(stmt="math.pow(3,3)", setup="import math"))
8th Apr 2020, 9:40 PM
rodwynnejones
rodwynnejones - avatar
+ 1
If I understand your question correctly : at times, functions may use less CPU time than methods, but only some times. If execution speed is an issue, you can setup timers and compare which will be faster in your case
10th Apr 2020, 7:29 PM
Radu Purcel