Instructor solution
5.2 Dunder Methods - Lesson
You may exit out of this review and return later without penalty.
Unit 5: Object-Oriented Programming (OOP) - Review Assignment
1.What is object-oriented programming?
2.Define a class called Person with an initializers that accepts two non-keyword arguments, first_name and last_name, and sets these arguments as properties of the object (with the same names).
3.Add a method get_product() to CustomA that returns the product of the properties a and b of an instance of CustomA.
4.What are private variables / methods?
5.What is name mangling?
6.What is a dunder method?
7.Add a dunder method to the Fruit class below that will consider two Fruit objects to be equal if they have the same values for their fruit_type and weight values.
8.Add a dunder method to the Fruit class below that will return the weight property of a Fruit object when it is passed into Python's built-in len() function.
9.Add a dunder method to the Fruit class below so that, when two Fruit objects are added together via the + operator (a) the sum of their grams properties is returned, if the two objects have the same value for their fruit_type properties, or (b) 0 is returned, otherwise.
10.Add a dunder method to the Fruit class below so that printing an object of Fruit prints the string "This <fruit_name> weighs <weight> grams." (replacing <fruit_name> and `
11.What is multilevel inheritance?
12.What is inheritance in object-oriented programming?
13.What is multiple inheritance?
14.What does the super() function do within a class?
15.What is Method Resolution Order, or MRO, in Python?
16.What does it mean for a class to override an inherited attribute?
17.Write a class C that inherits from both classes A and B. (C does not need to have any additional attributes beyond what it inherits.)
18.Write a class Square that inherits from the provided class Rectangle. Square should have an initializer that accepts one argument width and calls the initializer of Rectangle with this width value as both parameters. Square should also override the get_shape() method of Rectangle to return "square".
19.Write three empty classes, A, B, and C, where C inherits from B and B inherits from A.
20.Write a class C that inherits from the given classes A and B. This class should be empty, but calling the get_description() method from an object of C should return "B".
21.What is a generator function in Python?
22.Give an example of when you might want to use a generator function in Python.
23.Write a generator function even_to_n(n) that yields all even numbers from 0 (inclusive) to n (inclusive).
24.What methods must an iterator implement?
25.Add the appropriate methods to the class A provided so that iterating on an object of A iterates through the elements of A.vals.
You may exit out of this review and return later without penalty.