I'm a new learner of python/programming. Here is a question on top of head about the use of function in python.
If I had a list called myList.
- (a) If I were to sort it, I would use
myList.sort() - (b) If I were to sort it temporarily, I would use
sorted(myList)
Note the difference between the use of two functions, one is to apply the function to myList, the other one is use myList as a parameter to the function.
My question is, each time when I use a function.
- How do I know if the function should be used as an "action" to be applied to an object (in (a)), or
- should an object passed to the function as a parameter,(in (b)).
I have been confused with this for quite long time. appreciate any explanations.
Thanks.
myList.sort()is a method, butsorted(myList)is a functionlist.sortandsortedare two entirely different things. One is a method, the other a builtin function. It's not like any function can be used either asfoo(x)orx.foo().