---
tags: Python
---
# Python Self-Check 11
CS 1358 Introduction to Programming in Python
Fall Semester 2022
Prof. Pai H. Chou
Self-Check 11
Due Date: --
Answer the following questions to check your understanding of your material. Expect the
same kind of questions to show up on your tests. For this course, we use vi and vim
interchangeably.
## 1. Definitions and Short Answers
1. In Python, ArithmeticError is a base class of FloatingPointError, OverflowError, and ZeroDivisionError. So,
```
a. Does ArithmeticError inherit from FloatingPointError ? Or does FloatingPointError inherit from ArithmeticError?
//FloatingPointError inherit from ArithmeticError
b. Does FloatingPointError inherit from OverflowError ? Or ZeroDivisionError? Or is there any inheritance relationship between them?
//no
c. Is ZeroDivisionError a superclass of ArithmeticError? Or the other way around?
//subclass
```
2. Let a, f, o, and z respectively denote an instance of ArithmeticError, FloatingPointError, OverflowError, and ZeroDivisionError.
```
a. Is a an instance of FloatingPointError?
//No
b. Is f an instance of ArithmeticError?
//Yes
c. Does z inherit from ZeroDivisionError? Or what is the correct word for the relationship?
//z is a instance of ZeroDivisionError
d. Does o inherit from ArithmeticError?
//Yes
```
3. Which of the following evaluates to True?
```
a. isinstance(f, ArithmeticError) //T
b. isinstance(z, FloatingPointError) //F
c. isinstance(a, ZeroDivisionError) //F
d. issubclass(OverflowError, ArithmeticError) //T
e. issubclass(FloatingPointError, ZeroDivisionError) //F
f. issubclass(ArithmeticError, ZeroDivisionError) //F
```
4. Suppose you want to define a class named MyList by subclassing from the built-in list class
```
class MyList(list):
def __repr__(self):
return self.__class__.__name__ + '(' + _____ + ')'
```
```
a. This class does not define the __init__() method. Does this mean you can't call MyList as a constructor? If you can call MyList as a constructor, what method is actually called?
//No, call list constructor
b. By defining the __repr__() method in MyList class, what happens to the base class's (i.e., list class's) __repr__() method? Is it replaced? Or does it continue to exist?
//it continue to exist but when you call __repr__() it will be the new one
c. How does MyList's __repr__() method invoke its superclass list's __repr__() method to render the actual list content as constructor argumen0t?
//super().__repr__()
```
5. If you define a find() method in MyList class,
```
a. does it need to call the list's find() method?
//no
b. Does it automatically call list's find() method?//no
i. If so, is list's find() method automatically called before or after MyList's find() method?
ii. If not, how can MyList's find() call its base class's find()?
//super().find()
```
6. MyList's sort() method is defined as follows:
```
class MyList(list):
# __repr__() and find() not shown
def sort(self):
D ={'NoneType': 0, 'int': 1, 'float': 1, 'str': 2,
'tuple': 3, 'list': 4}
return super().sort(key=lambda x: \
(D.get(type(x).__name__, 5), x)),
reverse=reverse)
# additional definition not shown
```
```
a. What does type(x).__name__ do? What is the value of
i. type(3).__name__? //int
ii. type((12, 34)).__name__? //tuple
iii. type('hello').__name__? //str
iv. type({}).__name__? //dict
b. Given the value of D defined on lines 4-5, what does D.get(k, v) do and how is it different from D[k]?
//if D don't have key k D[k] will error, D.get return None or value you set up
What is the value of
i. D.get(type(3).__name__, 5) //1
ii. D.get(type((12, 34)).__name__, 5) //3
iii. D.get(type('hello').__name__, 5) //2
iv. D.get(type([]).__name__, 5) //4
v. D.get(type(3+2j).__name__, 5) //5
vi. D.get(type({}).__name__, 5) //5
c. Let
k = lambda x:(D.get(type(x).__name__, 5),x)
L = [7.4, 2, 'world', 'bye', (13, 24), (14, 28), None]
, then what is the value of
list(map(k, L))
//[(1, 7.4), (1, 2), (2, 'world'), (2, 'bye'), (3, (13, 24)), (3, (14, 28)), (5, None)]
?
and what is the result of
list(sorted(map(k, L)))
//[(1, 2), (1, 7.4), (2, 'bye'), (2, 'world'), (3, (13, 24)), (3, (14, 28)), (5, None)]
```
7. In the revised version of MyList class's sort method,
```
class MyList(list):
# __repr__() and find() not shown
def sort(self, key=None, reverse=False):
D ={'NoneType': 0, 'int': 1, 'float': 1, 'str': 2,
'tuple': 3, 'list': 4}
return super().sort(key=lambda x: \
(D.get(type(x).__name__, 5), \
key(x) if key is not None else x),\
reverse=reverse)
# additional definition not shown
```
```
a. What is the meaning of line 9, reverse=reverse in this context?
call sort built-in function with parameter name reverse and its value is reverse
b. If the caller does not pass a key parameter on line 3, then what is the value of the expression
lambda x: key(x) if key is not None else x
//lambda x:x
c. if the caller passes a callable object to the key parameter on line 3, then what is the value of the expression
lambda x: key(x) if key is not None else x
//lambda x:key(x)
```
8. In the ColorPoint class:
```
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
def __repr__(self):
return __class__.__name__ + \
repr((self._x, self._y))
class ColorPoint(Point):
def __init__(self, x, y, color):
super().__init__(x, y)
self._color = color
def __repr__(self):
return __class__.__name__ +\
repr((self._x, self._y, self._color))
```
```
a. What is the purpose of line 10?
//call constructor of superclass(Point)
b. After line 10, what attributes are defined in self?
//self._x self._y
c. If ColorPoint class doesn't define its own __repr__ but instead chooses to inherit it, what will be printed on the line below?
>>> p = Point(2, 3)
>>> p
Point(2, 3)
>>> q = ColorPoint(4, 5, 'black')
>>> q
____________
Does it print Point(4, 5)? ColorPoint(4, 5)? or something else?
//Point(4, 5)
```
9. What is the meaning of polymorphism in a programming language like Python? Does it mean an object can take on different names? Or does it mean a name can refer to one of different possible objects?
```
No, it means object have same name that encompasses many different models
```
10. Why is the built-in str() considered an overloaded function?
```
str() can input int float list etc.
```
11. What is the meaning of operator overloading?
```
operator can use in other data type
```
12. To overload operators +, -, *, / for the Point class above, what do you have to declare?
```
+:__add__()
-:__sub__()
*:__mul__()
/:__truediv__()
```
13. Assume x = 3, what are the values of the following expressions, if valid? If not valid, why not, and how can it be fixed?
```
a. x.__add__(2) //5
b. 2.__add__(x) //not vaild, (2).__add__(x)
c. x.__add__(2.) //not vaild, x.__add__(2)
d. 2..__add__(x) //5.0
```
14. In the Vector class
```
import operator as op
class Vector:
def __init__(self, *v):
self._v = list(v) # covert tuple to list
def __repr__(self):
return __class__.__name__+repr(tuple(self._v))
def __add__(self, right):
return Vector(*map(op.add, self._v, right._v))
# op.add is same as lambda x,y: x+y
def __sub__(self, right):
return Vector(*map(op.sub, self._v, right._v))
x = Vector(1, 2, 3)
y = Vector(4, 5, 6)
z = x + y
i = id(x)
x += y
j = id(x)
print(i == j)
```
```
a. When x = Vector(1, 2, 3) is called, what is the value of parameter v on line 3?
//(1, 2, 3)
b. What is the equivalent method syntax when line 13 z = x + y is executed? In other words, the statement can be written in the form of
z = object.method(arg)
//z = x.__add__(y)
What are the object, method, and arg?
//x y z is obj, y is arg, __add__() is method
c. By the time line 13 z = x + y finishes execution, how many times has the Vector constructor been called?
//1 if in line 13 and 3 if in program before line 13
d. Does line 17 print True or False? Explain.
//No, it will occur error, because == operator don't define Vector
```
15. In the previous problem, Python understands how to execute line 15
15 x += y
as
x = x.\_\_add__(y)
So why would you ever need to overload the \_\_iadd__(self, other) method? Isn't it redundant?
```
if use x = x.__add__(y) will more garbage, less efficient
```
16. Assume x and y refer to the two Vector instances. In order to support the following operator syntax, what special methods must be defined in the Vector class, and what is the equivalent object.method(args) syntax? Fill in the last column of the table below.
| operator syntax | example | equivalent object.method(args) syntax |
| -------- | -------- | -------- |
| indexing | x[3] | x.\_\_getitem__(3) |
|slicing |x[2:5] | x.\_\_getitem__(slice(2, 5))|
|indexed assignment |x[1] = 5 | x.\_\_setitem__(1, 5) |
|sliced assignment |x[0:2] = (8, 6) | x.\_\_setitem__(slice(0, 2), (8, 6))|
17. Given that binary operators << and >> have lower precedence than binary operators + and -, and all are left associative, in what order does Python evaluate the expression
```
m + n << p - q >> r
```
? is it
```
a. ((m + n) << (p - q)) >> r
b. (m + (n << p)) - (q >> r)
c. (m + n) << ((p - q) >> r)
d. m + ((n << p) - (q >> r))
or some other way?
//a
```
18. Both str() and repr() return a string of an object. What is their difference? Suppose you have x = 'hello\n', what is the value of
```
list(str(x)) //['h', 'e', 'l', 'l', 'o', '\n']
list(repr(x)) //["'", 'h', 'e', 'l', 'l', 'o', '\\', 'n', "'"]
?
```
19. If you overload the \_\_len__() special method in your Vector class, how does Python expect the user to call it on an instance v? (Hint: not v.\_\_len__())
```
len(v)
```