zoukankan      html  css  js  c++  java
  • [小甲鱼]入门学习python笔记 【魔法方法】

    //__new__(cls[,....])
    //对象实例化调用的第一个方法,它的第一个参数是这个类,而其他参数会直接传递给__init__()方法
    //需要在基类的基础上对其进行修改时重写__new__()方法

    //__del__()方法
    //只有在该类实例化的对象全部被del掉时,才调用__del__()方法

    //python 中的运算符重载
    
    class New_int(int):   //基于基类int的子类New_int
        def__add__(self,other)    //重载运算符 +
            return int.__sub__(self,other)   //返回基类的减法运算
    
    class New_Int(int):
        def__add__(self,other):
            return int(self)-int(other)   // 等价于int. __sub__(self,other)
    
    >>>a = New_Int(1)
    >>>b = New_int(3
    //反运算
    //左操作数不支持相应的操作时右操作数的反运算方法会被调用。
    
    >>> class Newint(int):
        def __add__(self,other):
            print("add 被调用")
        def __radd__(other,self):
            print("radd 被调用")
    
    >>> a = Newint(5)
    >>> b= Newint(6)
    >>> a + b
    add 被调用
    >>> 1+a
    radd 被调用
  • 相关阅读:
    python 爬虫 urllib模块 url编码处理
    python 爬虫 urllib模块 目录
    python 爬虫 urllib模块介绍
    python 爬虫 目录
    爬虫 介绍
    POJ 2533
    POJ 2531
    POJ 2524
    POJ 2505
    POJ 2521
  • 原文地址:https://www.cnblogs.com/Latticeeee/p/8643774.html
Copyright © 2011-2022 走看看