1

I have had some struggles understanding Python's built-in functions and methods. From what I understand, functions return information about something whereas methods change something. Is this correct?

Also not clear to me is why some functions and methods require parameters while others do not. Is this requiring-parameter quality specific to either functions or methods?

Finally, why do some of these built-in functions/parameters simply state their name followed by parameters (eg sorted(list)) while others (eg list.sort()) use a period in their syntax?

2OL1
  • 87
  • 7
  • 1
    Methods are class attributes that are invoked through the class. A normal method is invoked through a class instance, while a class method is invoked through the class itself. Methods are special cases of functions. Functions may exist independently of any class. – Tom Karzes Aug 22 '20 at 01:09
  • 1
    "From what I understand, functions return information about something whereas methods change something." No, that isn't the correct distinction. – juanpa.arrivillaga Aug 22 '20 at 02:23

1 Answers1

0

A method is a function which is applicable to a certain class, while a function can be used in any valid class. Like the sort method for the list class sorts the list. Methods of mutable types mostly change the item, so list.sort would set the value of list to the sorted value of list and return None. But methods of immutable types like strings will return a new instance of the thing as seen below.

question = "How is this?"
question.replace("How", "What")        # Returns "What is this", but does not change question.
print(question)                        # Prints "How is this?"
print(question.replace("How", "What")) # Prints "What is this"

Built in functions like sorted do not change the item, they return a new version, or instance, of it.

list1 = [4,3,6,2]
sorted(list1)    # Returns [2,3,4,6], but does not modify list.
print(list1)     # Prints [4,3,6,2]
list1.sort()     # Returns None, but changes list.
print(list1)     # Prints [2,3,4,6]

When you use a method, you put a period after the variable to show that it can only be used for that specific class. Why some functions require arguments while some methods don't - like sorted(list) requires list, but list.sort() doesn't require arguments, is that when you use a method on a class, Python by default passes in a parameter called self, which is the actual variable, list in this case. If you have worked with JavaScript, self is something like the this keyword in JS.

So when you enter list.sort(), Python is actually running the function sort inside the list class passing it a parameter of self.

Safwan Samsudeen
  • 1,645
  • 1
  • 10
  • 25
  • 1
    I wouldn't say "most methods actually change the item"; the rule is just that logically mutating methods *either* return a new mutated instance (leaving the original unchanged) *or* return `None` and mutate the original instance. Obviously, all methods on immutable types (`str`, `bytes`, `tuple`, `int`, etc.) don't change the instance they're called on, which by itself is enough non-mutating methods to put the lie to "most methods change the item". – ShadowRanger Aug 22 '20 at 01:21
  • The `self` parameter as an argument is a helpful clarification. This brings up another confusion: aren't built-in functions and methods completely different from functions as a data structure and methods as part of the class data structure? In the above example with `list1`, there are no functions nor classes defined, the code simply begins with a list... – 2OL1 Aug 22 '20 at 01:38
  • @ShadowRanger, you are right, I kind of got caught up with the list class... I have edited the answer. – Safwan Samsudeen Aug 22 '20 at 02:45
  • @2OL1, for some built in classes like lists and strings, you don't have to actually say the class and declare it like we do with out own classes. When you write `list1 = [...]`, you are actually assigning `list1` to an item with the `list` class. Python defines all these classes with their functions for you. – Safwan Samsudeen Aug 22 '20 at 02:51