Object-oriented programming is all about defining our own data types and the operations they support. However, there are some limitations! Consider the code:
print(5)
Python knows exactly how to print this integer. More precisely, it knows how to internally convert that integer into its string representation, and then print that.
However, if you were to write something like:
s = Student('Example McStudent', [90, 100, 95]) # using the Student class from Lab 11, Part E
print(s)
...how is Python supposed to know how you would like this printed? It doesn't know anything about the Student data type beyond what you've provided in the class definition. In Part E of Lab 11, we got around this by defining our own print method for the class, so we could do s.print() to print the student in exactly the format we desired. In Lab 12, we'll solve this problem differently: by defining a special method of our class that will cooperate with Python's provided print function.
Printing isn't the only problem! Again, with Python's provided data types, Python knows exactly what to do with something like
if 100 > 87:
or
if 'Los Angeles' < 'San Diego'
because it knows how to compare integers and strings. But if what if you tried to compare two Student objects? Python will throw up its hands: how is it supposed to know what makes one student "less than" another? In Lab 12, we'll define another special method that tells Python how the < operator works if it's comparing two objects of our new class. There are similar methods for other operators, but we won't cover those in this lab.
In this part of the lab, you will implement a City class. Objects of this class have instance variables for the city's name (a string) and population (an integer). You will implement the following methods:
"""
Program: Lab 12
Author: Your name
Define and test a basic City class.
"""
import sys
class City:
pass
def main():
tokyo = City('Tokyo', 13189000)
mexico_city = City('Mexico City', 8857188)
main()

print(tokyo) print(mexico_city)Run your program. You should see an output that looks something like
<__main__.City object at 0xe5ec10> <__main__.City object at 0xe5ecd0>This is because we haven't told Python how to represent a city as a string.
Tokyo (pop. 13189000)or
Mexico City (pop. 8857188)Rerun your program. You should see the two lines of output above.
# Print whichever city is larger
print('The larger city is:')
if mexico_city < tokyo:
print(tokyo)
else:
print(mexico_city)
Run your program. It should fail because we haven't told Python what it means for one City object to be smaller than another.def __lt__(self, other):
# Return True if the population of self is less than
# the population of other and return False otherwise
pass
Tokyo (pop. 13189000) Mexico City (pop. 8857188) The larger city is: Tokyo (pop. 13189000)