当前位置: 首页 > news >正文

魔术方法介绍

目录

一、基本介绍

1、什么是魔术方法

2、常见的魔术方法

二、__str__

1、基本介绍

2、应用实例:请输出Monster对象的属性信息

三、__eq__

1、基本介绍

2、应用实例

四、其它几个魔术方法

1、其它魔术方法

2、应用实例


参考文档:3. 数据模型 — Python 3.12.5 文档

一、基本介绍

1、什么是魔术方法

1)在Python中,所有以双下划线__包起来的方法,统称为Magic Method(魔术方法),它是一种特殊的方法,普通方法需要调用,而魔术方法不需要调用就可以自动执行

2)魔术方法在类或对象的某些事件发生时会自动执行,让类具有神奇的“魔力”。如果希望根据自己的程序定制特殊功能的类,那么就需要对这些方法进行重写

3)Python中常用的运算符、for循环、以及类操作等都是运行在魔术方法之上的

2、常见的魔术方法

二、__str__

1、基本介绍

1)打印对象默认返回:类型名+对象内存地址,子类往往重写__str__,用于返回对象的属性信息

2)重写__str__方法,print(对象)或str(对象)时,都会自动调用该对象的__str__

2、应用实例:请输出Monster对象的属性信息

class Monster:def __init__(self,name,age,gender):self.name=nameself.age=ageself.gender=genderm=Monster("青牛怪",500,"男")
print(m)  # 默认输出类型+地址

class Monster:def __init__(self,name,age,gender):self.name=nameself.age=ageself.gender=gender"""1.在默认情况下,调用的是父类object的__str__2.父类object的__str__返回的就是 类型+地址3.可以根据需要重写__str__"""# 重写__str__def __str__(self):# return super().__str__()return f"{self.name} {self.age} {self.gender}"m=Monster("青牛怪",500,"男")
print(m)  # 重写__str__之后输出对象的属性信息

三、__eq__

1、基本介绍

1)==是一个比较运算符:对象之间进行比较时,比较的是内存地址是否相等,即判断是不是同一个对象

2)重写__eq__方法,可以用于判断对象内容/属性是否相等

2、应用实例

1)判断两个Person对象的内容是否相等,如果两个Person对象的各个属性值都一样,则返回true,反之false

2)Person类,属性(name,age,gender)

class Person:def __init__(self,name,age,gender):self.name=nameself.age=ageself.gender=genderp1=Person("smith",21,"男")
p2=Person("smith",21,"男")
# == 默认是比较两个对象的地址是否相同
print(f"p1==p2:{p1==p2}")  # False
class Person:def __init__(self,name,age,gender):self.name=nameself.age=ageself.gender=gender# 重写__eq__def __eq__(self, other):# 判断other是不是Personif isinstance(other,Person):return self.name==other.name and \self.age==other.age and \self.gender==other.genderreturn False# 没有重写__eq__前,==比较的是内存地址
# 重写__eq__后,==比较的是属性/内容
p1=Person("smith",21,"男")
p2=Person("smith",21,"男")
print(f"p1==p2:{p1==p2}")  # True

四、其它几个魔术方法

1、其它魔术方法

2、应用实例

1)根据Person对象的年龄进行比较大小,重写相应的魔术方法

2)Person类,属性(name,age,gender)

class Person:def __init__(self,name,age,gender):self.name=nameself.age=ageself.gender=gender# 重写__eq__def __eq__(self, other):# 判断other是不是Personif isinstance(other,Person):return self.name==other.name and \self.age==other.age and \self.gender==other.genderreturn False# 重写__lt__def __lt__(self, other):# 判断other是不是Personif isinstance(other, Person):return self.age<other.agereturn "类型不一致,不能比较"# 重写__lt__def __le__(self, other):# 判断other是不是Personif isinstance(other, Person):return self.age<=other.agereturn "类型不一致,不能比较"# 重写__ne__def __ne__(self, other):return not self.__eq__(other)p1=Person("smith",21,"男")
p2=Person("smith",21,"男")
print(f"p1<p2:{p1<p2}")  # False
print(f"p1<=p2:{p1<=p2}")  # True
print(f"p1!=p2:{p1!=p2}")  # False


http://www.mrgr.cn/news/21997.html

相关文章:

  • js window.addEventListener 是什么?
  • MVVM 基础
  • 【Qt】定时器事件
  • 【C++】std::list
  • 微信小程序授权登录流程以及使用到的API
  • 如何快速练习键盘盲打
  • 【Rocketmq入门-基本概念】
  • 【web网页制作】html+css旅游家乡河南主题网页制作(5页面)【附源码】
  • C语言小游戏--贪吃蛇实现
  • (南京观海微电子)——GH7006 Application Note
  • C++学习笔记----6、内存管理(四)---- 通常的内存陷阱(2)
  • python内置模块datetime.time类详细介绍
  • nginx 上部署 vue 项目,注意这几个细节,性能会好不少
  • PostgreSQL中的多版本并发控制(MVCC)深入解析
  • 验证码的作用,为什么要存在验证码?
  • 嵌入式 24081开班典礼:与梦同行,同筑未来
  • excel翻译软件有哪些?如何高效提翻译?
  • 【C语言】插入排序、希尔排序——动图展示
  • 半年高达552亿元,锁定云第一,中国电信天翼云紧追不舍
  • python的统计分析库scipy.stats使用方法