Object-oriented programming
Methods
A pizza is more than its size, price, and toppings. You can add a topping, compare it with another pizza, or print it. Those are its behaviors, and in Python they are written as methods inside the class.
1Methods are behaviors
An object's attributes hold its data. Its methods are the things it can do. A method is a function written inside a class. It looks like an ordinary function with two differences: it is indented under the class line, and its first parameter is always self.
Here is a Pizza class with its first method, describe. The constructor is the same one you wrote earlier. Run the cell.
You call a method with a dot, the method name, and parentheses. You do not pass self. When you write p.describe(), Python passes p as self automatically, so inside the method self.size is the size of p. The same call can be written out in full, with the class name and the object passed by hand.
The output says method, and no description was printed. Write p.describe() to run it.
2Methods that change an object
A method can change the object's own attributes through self. The class below gains two new methods, marked # new. The method add_topping changes the pizza. It takes one argument, the topping, after self. The method price_with_tip works out a value and hands it back with return.
After add_topping, the pizza has two toppings and costs 17. The method itself shows nothing. It only changes the object, which is why the describe call comes after it.
Printing is not returning
You saw this difference with functions. It applies to methods in the same way. The method describe prints text and returns nothing, so what it gives back is None. The method price_with_tip returns a number, which you can store and use in later calculations.
Predict the output of the next cell. The line b = a does not make a second pizza.
Show the answer
It prints ['onion'] and then 10. The names a and b refer to the same pizza, so adding a topping through b changes the pizza that a also sees. A method that changes self changes the object itself, not a copy.
3Methods that use another object
A method can take another object of the same class as an argument. That lets two objects work together. The method cheaper_than below compares the pizza before the dot with the pizza in the parentheses.
The name other is an ordinary parameter name, chosen to read well. In lunch.cheaper_than(party), self is lunch and other is party. Inside the method, self.price and other.price are the two prices being compared.
lunch.cheaper_than(party), what do self and other refer to?4Printing an object
By default, printing an object shows only its class name and a memory address, which is not useful. A special method named __str__ changes that. Like __init__, you never call it yourself. Python runs it whenever the object needs to be shown as text, for example by print. It must return a string.
The same text appears whenever the pizza is printed or placed inside an f-string. This version replaces describe, since print(p) now does the job.
Show the fix
The method printed its text, and then returned None, so Python complained that __str__ returned something that is not a string. Replace print(...) with return ....
5Practice
Exercise 1: remove a topping
Finish the method remove_topping. If the topping is on the pizza, it removes the topping, lowers the price by 1, and returns True. If the topping is not there, it changes nothing and returns False.
Hint
Check whether the topping is on the pizza with topping in self.toppings. If it is, remove it with the list method .remove(topping), lower self.price by 1, and return True. Otherwise return False and change nothing.
Show a solution
def remove_topping(self, topping):
if topping in self.toppings:
self.toppings.remove(topping)
self.price -= 1
return True
return False
Exercise 2: compare two pizzas
Write the method bigger_than(self, other). It returns True if this pizza's size is larger than the other pizza's size, and False otherwise. The dictionary SIZE_RANK gives each size a number.
Hint
Here self is the pizza before the dot, and other is the pizza in the parentheses. Look up the rank of each size with SIZE_RANK[self.size] and SIZE_RANK[other.size], then compare the two numbers.
Show a solution
def bigger_than(self, other):
return SIZE_RANK[self.size] > SIZE_RANK[other.size]