Methods#
Methods#
Methods Generally#
A method is a function applied directly to the object you call it on.
The general form of a method is what you see here, where the object you are operating on is followed by a period, the name of the method and then parentheses. The parentheses indicate that code is being executed. As we’ll see methods are secific types of functions that operate directly on an object.
object.method()
In other words: methods “belong to” an object.
Method: append
#
For example, here we see the append
list method. When we specify to append
to the my_list
list, this executes the code in the existing list method append
. This method adds whatever is being appened (in this case, the integer 4
) to the end of my_list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
[1, 2, 3, 4]
Importantly, since methods “belong” to a specific object type…they only work on the object that they’re “attached” to. If you try to use append
– a list method – on a tuple, you will get an error, as append
only operates on lists.
# this code will error
my_tup = (1, 2, 3)
my_tup.append(4)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[2], line 3
1 # this code will error
2 my_tup = (1, 2, 3)
----> 3 my_tup.append(4)
AttributeError: 'tuple' object has no attribute 'append'
String, List, & Dictionary Methods#
While there are many more methods than we’ll introduce here, we want you to get a sense of what methods exist for each variable type and start to think of why these are “attached” to these particular object types. It should become clear that string methods, for example, are operations you would commonly carry our on strings, and thus, that’s why these methods exist for strings. The same logic follows for other variable types.
String Methods#
Here we see that lower()
makes all of the characters in the string it is operating on lowercase, while upper()
does the opposite. capitalize
on the other hand does title case capitalization, capitalizing the first letter in the string, for example:
# Make a string all lower case
'aBcD'.lower()
'abcd'
# Make a string all upper case
'aBcD'.upper()
'ABCD'
# Capitalize a string
'python is great'.capitalize()
'Python is great'
List Methods#
When it comes to list methods, we’ve already introuced append()
which adds an element to the end of a list, but we also introduce the sort()
method here. By default, sort()
sorts values alphanumerically from lowest to highest. However, note that there is a reverse
parameter that allows you to sort in the opposite direction – from highest to lowest.
# sort sorts integers in numerical orders
ints = [16, 80, 33, 40]
ints.sort()
ints
[16, 33, 40, 80]
ints.sort(reverse=True)
ints
[80, 40, 33, 16]
# append adds to the end of a list
ints.append(50)
ints
[80, 40, 33, 16, 50]
# reverse order of list
ints.reverse()
ints
[50, 16, 33, 40, 80]
Dictionary Methods#
To introduce a few dictionary methods, here we see keys()
, which extracts the keys from a dictionary and values()
which extracts the values.
car = {
"brand": "BMW",
"model": "M5",
"year": 2019
}
# keys() returns the keys of a dictionary
car.keys()
dict_keys(['brand', 'model', 'year'])
# values() returns the keys of a dictionary
car.values()
dict_values(['BMW', 'M5', 2019])
Methods: In Place vs Not In Place#
List methods that are in place#
In place methods directly operate on the object, meaning they change the contents of the object directly, without assigning those changes back to hte original variable.
# Reverse a list
my_list = ['a', 'b', 'c']
my_list.reverse()
print(my_list)
['c', 'b', 'a']
Dictionary methods that are not in place#
Methods that do not operate in place, on the other hand, do not change the object they’re operating on. Here, car
does not change when using the keys()
method, for example.
car
{'brand': 'BMW', 'model': 'M5', 'year': 2019}
# Return the keys in the dictionary
car.keys()
dict_keys(['brand', 'model', 'year'])
# car has not changed
car
{'brand': 'BMW', 'model': 'M5', 'year': 2019}
Correspondance Between Functions & Methods#
All methods are functions. Methods are special functions attached to a variable type. All functions are NOT methods.