Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
For each line of code, name the special method that Python calls.
str(p)p1 == p2len(p)p1 + p2sorted([p1, p2])x in pHave you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Each syntax has a special method behind it.
str(p) (and print(p)): __str__.p1 == p2: __eq__.len(p): __len__.p1 + p2: __add__.sorted([p1, p2]): __lt__, which asks which comes first.x in p: __contains__.Check. Look at the picture of the special methods, and match each row.
Answer: __str__, __eq__, __len__, __add__, __lt__, __contains__.
Log in to join the discussion.
Loading…