Python
  • 为什么学Python?
  • Python代码规范
    • 简明概述
    • 注释
    • 命名规范
  • 第一个Python程序
    • Python 简介
    • Python 的安装
    • 第一个 Python 程序
    • 集成开发环境(IDE): PyCharm
  • 基本数据类型和变量
    • Python 语法的简要说明
    • print() 函数
    • Python 的基本数据类型
    • 字符串的编码问题
    • 基本数据类型转换
    • Python 中的变量
  • List 和 Tuple
    • List(列表)
    • tuple(元组)
  • Dict 和 Set
    • 字典(Dictionary)
    • set
  • 条件语句和循环语句
    • 条件语句
    • 循环语句
    • 条件语句和循环语句综合实例
  • 函数
    • Python 自定义函数的基本步骤
    • 函数返回值
    • 函数的参数
    • 函数传值问题
    • 匿名函数
  • 迭代器和生成器
    • 迭代
    • Python 迭代器
    • lsit 生成式(列表生成式)
    • 生成器
    • 迭代器和生成器综合例子
  • 面向对象
    • 面向对象的概念
    • 类的定义和调用
    • 类方法
    • 修改和增加类属性
    • 类和对象
    • 初始化函数
    • 类的继承
    • 类的多态
    • 类的访问控制
  • 模块与包
    • Python 模块简介
    • 模块的使用
    • 主模块和非主模块
    • 包
    • 作用域
  • Python 的 Magic Method
    • Python 的 Magic Method
    • 构造(__new__)和初始化(__init__)
    • 属性的访问控制
    • 对象的描述器
    • 自定义容器(Container)
    • 运算符相关的魔术方法
  • 枚举类
    • 枚举类的使用
    • Enum 的源码
    • 自定义类型的枚举
    • 枚举的比较
  • 元类
    • Python 中类也是对象
    • 使用 type() 动态创建类
    • 什么是元类
    • 自定义元类
    • 使用元类
  • 线程与进程
    • 线程与进程
    • 多线程编程
    • 进程
  • 一步一步了解正则表达式
    • 初识 Python 正则表达式
    • 字符集
    • 数量词
    • 边界匹配符和组
    • re.sub
    • re.match 和 re.search
  • 闭包
  • 装饰器
  • 知识点补漏
    • Python 关键字 yield
  • Python 进阶部分
  • 使用Python虚拟环境
  • Mac中使用virtualenv和virtualenvwrapper
  • Django
Powered by GitBook
On this page
  • 1、比较运算符
  • 2、算术运算符
  1. Python 的 Magic Method

运算符相关的魔术方法

运算符相关的魔术方法实在太多了,j就大概列举下面两类:

1、比较运算符

魔术方法
说明

__cmp__(self, other)

如果该方法返回负数,说明 self < other; 返回正数,说明 self > other; 返回 0 说明 self == other 。强烈不推荐来定义 __cmp__ , 取而代之, 最好分别定义 __lt__, __eq__ 等方法从而实现比较功能。 __cmp__ 在 Python3 中被废弃了。

__eq__(self, other)

定义了比较操作符 == 的行为

__ne__(self, other)

定义了比较操作符 != 的行为

__lt__(self, other)

定义了比较操作符 < 的行为

__gt__(self, other)

定义了比较操作符 > 的行为

__le__(self, other)

定义了比较操作符 <= 的行为

__ge__(self, other)

定义了比较操作符 >= 的行为

来看个简单的例子就能理解了:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

class Number(object):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        print('__eq__')
        return self.value == other.value

    def __ne__(self, other):
        print('__ne__')
        return self.value != other.value

    def __lt__(self, other):
        print('__lt__')
        return self.value < other.value

    def __gt__(self, other):
        print('__gt__')
        return self.value > other.value

    def __le__(self, other):
        print('__le__')
        return self.value <= other.value

    def __ge__(self, other):
        print('__ge__')
        return self.value >= other.value


if __name__ == '__main__':
    num1 = Number(2)
    num2 = Number(3)
    print('num1 == num2 ? --------> {} \n'.format(num1 == num2))
    print('num1 != num2 ? --------> {} \n'.format(num1 == num2))
    print('num1 < num2 ? --------> {} \n'.format(num1 < num2))
    print('num1 > num2 ? --------> {} \n'.format(num1 > num2))
    print('num1 <= num2 ? --------> {} \n'.format(num1 <= num2))
    print('num1 >= num2 ? --------> {} \n'.format(num1 >= num2))

输出的结果为:

__eq__
num1 == num2 ? --------> False

__eq__
num1 != num2 ? --------> False

__lt__
num1 < num2 ? --------> True

__gt__
num1 > num2 ? --------> False

__le__
num1 <= num2 ? --------> True

__ge__
num1 >= num2 ? --------> False

2、算术运算符

魔术方法
说明

__add__(self, other)

实现了加号运算

__sub__(self, other)

实现了减号运算

__mul__(self, other)

实现了乘法运算

__floordiv__(self, other)

实现了 // 运算符

___div__(self, other)

实现了/运算符. 该方法在 Python3 中废弃. 原因是 Python3 中,division 默认就是 true division

__truediv__(self, other)

实现了 true division. 只有你声明了 from __future__ import division 该方法才会生效

__mod__(self, other)

实现了 % 运算符, 取余运算

__divmod__(self, other)

实现了 divmod() 內建函数

__pow__(self, other)

实现了 ** 操作. N 次方操作

__lshift__(self, other)

实现了位操作 <<

__rshift__(self, other)

实现了位操作 >>

__and__(self, other)

实现了位操作 &

__or__(self, other)

实现了位操作 `

__xor__(self, other)

实现了位操作 ^

可以关注下公众号:

这个公号可能很少更新,但是一更新,就是把整理的一系列文章更新上去。

Previous自定义容器(Container)Next枚举类

Last updated 1 month ago