[Python]小甲鱼Python视频第042课(魔法方法:算术运算 )课后题及参考解答

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 21:43:00 2019

@author: fengs
"""



"""
0. 自 Python2.2 以后,对类和类型进行了统一,做法就是将 int()、float()、str()、list()、tuple() 这些 BIF 转换为工厂函数。请问所谓的工厂函数,其实是什么原理?
    
    就是类对象,调用他们的时候,利用输入创建一个对应的实例对象

1. 当实例对象进行加法操作时,会自动调用什么魔法方法?
    对象的 __add__ 方法
    


2. 下边代码有问题吗?(运行起来似乎没出错的说^_^)
class Foo:
        def foo(self):
                self.foo = "I love FishC.com!"
                return self.foo

>>> foo = Foo()
>>> foo.foo()
'I love FishC.com!'

方法名与实例对象的属性名重名,方法会被覆盖而无法调用,第一次可以调用,第二次就会报错了。

"""

"""
3. 写出下列算术运算符对应的魔法方法:

运算符                     对应的魔法方法
+                         __add__
-                         __sub__
*                         __mul__
/                         __truediv__
//                        __floordiv__
%                         __mod__
divmod(a, b)              __divmod__,除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
**                        __pow__()
<<                        __lshift__()
>>                        __rshift__()
&                         __and__()
^                         __xor__()
|                         __or__()
"""


"""
4. 以下代码说明 Python 支持什么风格?
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

弱类型语言,不同类型的使用运算符时会自动调用相应对象的算数符号算法进行计算
??????
---->鸭子类型是啥? https://fishc.com.cn/thread-51471-1-1.html
"""

"""
0. 我们都知道在 Python 中,两个字符串相加会自动拼接字符串,但遗憾的是两个字符串相减却抛出异常。因此,现在我们要求定义一个 Nstr 类,支持字符串的相减操作:A – B,从 A 中去除所有 B 的子字符串。
>>> a = Nstr('I love FishC.com!iiiiiiii')
>>> b = Nstr('i')
>>> a - b
'I love FshC.com!'
"""
class Nstr0(str):
    def __init__(self,value):
        self.value = str(value)
    def __sub__(self,other):
         return self.replace(other.value,'')
     
#a0 = Nstr0('I love you oooooo')
#b0 = Nstr0('o')
#print(a0-b0)

"""
1. 移位操作符是应用于二进制操作数的,现在需要你定义一个新的类 Nstr,也支持移位操作符的运算:
>>> a = Nstr('I love FishC.com!')
>>> a << 3
'ove FishC.com!I l'
>>> a >> 3
'om!I love FishC.c'
"""
class Nstr1(str):
    def __init__(self,value):
        self.value = str(value)
    def __lshift__(self,count):
          return  self.value[count:] + self.value[0:count]
    def __rshift__(self,count):
        return self.value[-count:] +    self.value[0:len(self.value) - count]

a1 = Nstr1('I love FishC.com!')
#print(a1 << 3)
#'ove FishC.com!I l'
print(a1 >> 3)
#'om!I love FishC.c'


"""
2. 定义一个类 Nstr,当该类的实例对象间发生的加、减、乘、除运算时,将该对象的所有字符串的 ASCII 码之和进行计算:
"""

class Nstr2(str):
    def __init__(self,value):
        self.value = str(value)
    def __add__(self,other):
        sum1 = 0;
        sum2 = 0;
        for each in self.value:
            sum1 += ord(each)
        for each in other.value:
            sum2 += ord(each)
        return sum1 + sum2
    
    def __sub__(self,other):
        sum1 = 0;
        sum2 = 0;
        for each in self.value:
            sum1 += ord(each)
        for each in other.value:
            sum2 += ord(each)
        return sum1 - sum2
            
    def __mul__(self,other):
        sum1 = 0;
        sum2 = 0;
        for each in self.value:
            sum1 += ord(each)
        for each in other.value:
            sum2 += ord(each)
        return sum1 * sum2
    def __truediv__(self,other):
            sum1 = 0;
            sum2 = 0;
            for each in self.value:
                sum1 += ord(each)
            for each in other.value:
                sum2 += ord(each)
            return sum1 / sum2
    def __floordiv__(self,other):
            sum1 = 0;
            sum2 = 0;
            for each in self.value:
                sum1 += ord(each)
            for each in other.value:
                sum2 += ord(each)
            return sum1 // sum2
    
a = Nstr2('FishC')
b = Nstr2('love')
print(a + b)
#899
print(a - b)
#23
print(a * b)
#201918
print(a / b)
#1.052511415525114
print(a // b)
#1

  

posted @ 2019-03-17 22:28  Alimy  阅读(383)  评论(0编辑  收藏  举报