第043讲: 魔法方法:算术运算2 | 学习记录(小甲鱼零基础入门学习Python)

(标答出处: 鱼C论坛)
《零基础入门学习Python》

测试题:
在这里插入图片描述
答:不会,只有当前面的对象没有__ add __方法的时候才会被调用反运算的魔法方法。例如:

class Nint ( int ) :
    def __radd__ (self , other) :
        return int.__sub__(self,other)

a = Nint (5)
b = Nint (3)
print (a + b)
print (1 + b)

运行结果:
在这里插入图片描述
在这里插入图片描述
答:只有当前面的对象没有对应的魔法方式时,才会掉用第二个对象的反运算方法。

例如:a+b,只有当a的__ add __方法没法调用时,才会调用b的 __ radd __方法。
在这里插入图片描述
答:使用super()这个BIF函数。

class A(object):
    def __init__(self, a=0):
        self.a = a
    def get(self):
        return self.a

class B(A):
    def __init__(self, b):
        super(B, self).__init__(b)
    def get(self):
        return super(B, self).get()

if __name__ == '__main__':
    b = B(10)
    print(b.get())

在这里插入图片描述
答:你可以先为基类定义一个别名,在类定义的时候,使用别名代替你要继承的基类。因此,当你想要改变基类的时候,只需要修改别名赋值的那个语句即可。顺便说一下,当你的资源是视情况而定的时候,这个小技巧很管用。

BaseAlias = BaseClass  # 为基类取别名
 
class Derived(BaseAlias):
    def meth(self):
        BaseAlias.meth(self)  # 通过别名访问基类

在这里插入图片描述
答:在类中直接定义的属性叫做静态属性(没有self.),需要调用的时候直接用 类名.属性名进行调用

class A(object):
    count = 0
    def __init__ (self) :
        A.count += 1
    def getcount (self) :
        return A.count

a = A()
print (a.getcount())

运行结果:
在这里插入图片描述
在这里插入图片描述
答:静态方法是类的特殊方法,静态方法只需要在 普通方法前面加上 @staticmethod 修饰符即可。 例:

class C:
    @staticmethod   #该修饰符表示static()是静态方法
    def static (arg1 , arg2 , arg3 ) :
        print (arg1 , arg2 , arg3 , arg1 + arg2 + arg3)

    def nostatic (self) :
        print ("I'm the fucking normal method")

静态对象的最大优点就是不会绑定到实例对象上,换而言之就是节省开销。

class C:
    @staticmethod   #该修饰符表示static()是静态方法
    def static (arg1 , arg2 , arg3 ) :
        print (arg1 , arg2 , arg3 , arg1 + arg2 + arg3)

    def nostatic (self) :
        print ("I'm the fucking normal method")

c1 =C()
c2 = C()
#静态方法只在内存中生成一个,节省开销
print (c1.static is C.static)
print (c1.nostatic is C.nostatic)
print (c1.static)
print (c2.static)
print (C.static)
#普通方法每个实例对象都有独立的一个,开销较大
print (c1.nostatic)
print (c2.nostatic)
print (C.nostatic)

运行结果;
在这里插入图片描述
可以看出在 静态方法只在内存中生成一个,节省开销,而普通方法每个实例对象都有独立的一个,开销较大

动动手:
在这里插入图片描述

class C:
    def __init__ (self,*args) :  # *arg表示不确定个数的参数
        if  not args :
            print ("并没有传入参数")
        else :
            print ("传入了%d个参数,分别是:"%len(args),end = ' ')
            for each in args :
                print (each , end = ' ')

c = C()
c = C(1,2,3)

运行结果:
在这里插入图片描述
在这里插入图片描述

class Word (str) :
    #储存单词的类,定义比较单词的几种方法
    
    def __new__ (cls , word) :
        #注意必须要使用__new__方法,因为str 是不可变类型
        #必须在创建的时候将他初始化 
        if ' ' in word :
            print ('Value contains the spaces.Trucating to first space.')
            word = word[ : word.index(' ')]
            return str.__new__(cls,word)
    
    def __gt__ (self , other ) :
        return len(str(self)) > len(str(other))
    def __lt__ (self , other ) :
        return len(str(self)) < len(str(other))

    def __ge__ (self , other ) :
        return len(str(self)) >= len(str(other))

    def __le__ (self , other ) :
        return len(str(self)) <= len(str(other))

w1 = Word ('I love Xmy')
w2 = Word ('xmy')
print (w1 > w2)
print (w1 < w2)
print (w1 >= w2)
print (w1 <= w2)

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38970783/article/details/88087677