Test questions:
0. From Python 2 After 2, classes and types are unified by converting BIF S such as int(), float(), str(), list(), tuple() into factory functions. What is the principle of the so-called factory function?
A: the factory function is actually a class object. When you call them, you actually create a corresponding instance object.
# a and b are instance objects of the factory function (class object) int >>> a = int('123') >>> b = int('345') >>> a + b 468
1. What magic method will be called automatically when the instance object is added?
A: when objects a and B are added (a + b), Python will automatically add according to the add magic method of object a.
2. Is there a problem with the following code? (it seems to run without error)
class Foo: def foo(self): self.foo = "I love FishC.com!" return self.foo >>> foo = Foo() >>> foo.foo() 'I love FishC.com!'
A: This is definitely a gentle trap. This kind of BUG is difficult to check, so we must pay attention to: the attribute name and method name of the class must not be the same! If the code is written like this, a BUG that is difficult to check will appear:
class Foo: def __init__(self): self.foo = "I love FishC.com!" def foo(self): return self.foo >>> foo = Foo() >>> foo.foo() Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> foo.foo() TypeError: 'str' object is not callable
3. Write the magic method corresponding to the following arithmetic operators:
operator | Corresponding magic method |
---|---|
+ | __ add __(self, other) |
- | __ sub __(self, other) |
* | __ mul __(self, other) |
/ | __ truediv __(self, other) |
// | __ floordiv __(self, other) |
% | __ mod __(self, other) |
divmod(a, b) | __ divmod __(a, b) |
** | __ pow__(self, other[, modulo]) |
<< | __ lshift__(self, other) |
>> | __ rshift__(self, other) |
& | __ and__(self, other) |
^ | __ xor__(self, other) |
| | __ or__(self, other) |
4. What style does Python support in the following code?
def calc(a, b, c): return (a + b) * c >>> a = calc(1, 2, 3) >>> b = calc([1, 2, 3], [4, 5, 6], 2) >>> c = calc('love', 'FishC', 3) >>> print(a) 9 >>> print(b) [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] >>> print(c) loveFishCloveFishCloveFishC
Answer: Python supports duck typing style.
Hands on:
0. We all know that in Python, the addition of two strings will automatically splice strings, but unfortunately, the subtraction of two strings throws an exception. Therefore, now we need to define an Nstr class to support string subtraction: A – B, and remove all substrings of B from A.
Example:
>>> a = Nstr('I love FishC.com!iiiiiiii') >>> b = Nstr('i') >>> a - b 'I love FshC.com!'
A: just reload the sub magic method.
class Nstr(str): def __sub__(self, other): return self.replace(other, '')
1. The shift operator is applied to binary operands. Now you need to define a new class Nstr, which also supports the operation of the shift operator:
>>> a = Nstr('I love FishC.com!') >>> a << 3 'ove FishC.com!I l' >>> a >> 3 'om!I love FishC.c'
A: just reload the lsshift and rsshift magic methods.
class Nstr(str): def __lshift__(self, other): return self[other:] + self[:other] def __rshift__(self, other): return self[-other:] + self[:-other]
2. Define a class Nstr. When addition, subtraction, multiplication and division occur between instance objects of this class, the sum of ASCII codes of all strings of this object is calculated:
>>> a = Nstr('FishC') >>> b = Nstr('love') >>> a + b 899 >>> a - b 23 >>> a * b 201918 >>> a / b 1.052511415525114 >>> a // b 1
class Nstr: def __init__(self, arg=''): if isinstance(arg, str): self.total = 0 for each in arg: self.total += ord(each) else: print("Parameter error!") def __add__(self, other): return self.total + other.total def __sub__(self, other): return self.total - other.total def __mul__(self, other): return self.total * other.total def __truediv__(self, other): return self.total / other.total def __floordiv__(self, other): return self.total // other.total
class Nstr(int): def __new__(cls, arg=0): if isinstance(arg, str): total = 0 for each in arg: total += ord(each) arg = total return int.__new__(cls, arg)