zoukankan      html  css  js  c++  java
  • 魔术方法

    魔术方法

        __str__  : 使用print打印时调用的方法

            一个小例子

        __repr__:交互模式下调试时调用的方法

            一个小例子(注意这个例子是在ipython中完成的)

     

        __call__: 让类可以像函数一样被使用

     

    以下魔法方法为了解即可,不常用


    类名 . __bases__  查看当前类的父类

     

    类名 . __doc__  or  实例名.__doc__ 返回当前类中的说明文档(''' '''三重引号包含的内容)

     

    类名 . __dict__  以字典形式返回当前类中的所有属性

     

    实例名 . __class__()   查看当前实例化的类的原始类名


    def __add__(self,other)    实例化类1 + 实例化类2  时运行的参数

    def __sub__(self,other)    实例化类1  - 实例化类2  时运行的参数

    def __mul__(self,other)    实例化类1  实例化类2  时运行的参数

    def __mod__(self,other)   实例化类1 % 实例化类2  时运行的参数

    def __iadd__(self,other)    实例化类1 += 实例化类2  时运行的参数

    def __isub__(self,other)    实例化类1  -= 实例化类2  时运行的参数

    def __imod__(self,other)   实例化类1 %= 实例化类2  时运行的参数

    def __imul__(self,other)    实例化类1  *= 实例化类2  时运行的参数

    def __radd__(self,other)    实例化类2  + 实例化类1  时运行的参数

    def __rsub__(self,other)   实例化类2  实例化类1  时运行的参数

        一个小例子

     

  • 相关阅读:
    洛谷 P2872 [USACO07DEC]道路建设Building Roads
    cogs 29. 公路建设
    cogs 7. 通信线路
    cogs 2478. [HZOI 2016]简单的最近公共祖先
    洛谷 P1342 请柬
    洛谷 P1186 玛丽卡
    洛谷 P1491 集合位置
    启动、停止、重启服务
    洛谷——P1025 数的划分
    洛谷——P3368 【模板】树状数组 2
  • 原文地址:https://www.cnblogs.com/shijin7/p/9130493.html
Copyright © 2011-2022 走看看