当前位置:网站首页>特殊成员与魔法方法
特殊成员与魔法方法
2022-04-23 06:19:00 【qq1033930618】
一、主函数
from module_a import Cat
# __init__() 构造函数
cat = Cat("Tom", 15)
# # __del__() 析构函数 显式调用或程序结束时自动调用
# del cat
# # print(cat) 不可打印
# __doc__ __modules__ __class__
print(cat.__doc__) # 打印类说明文档
print(cat.__module__) # 打印类所在模块名称
print(cat.__class__) # 打印类所在位置
# __call__()
cat(1, 2) # 使类像函数一样被调用
print(callable(cat))
# __dict__
print(cat.__dict__) # 以字典形式打印共有与私有成员属性
print(cat._Cat__private_name) # 访问私有成员属性
# __str__()
print(cat) # 打印对象返回一段特性的话 而不是访问地址
print(str(cat))
# __len__()
print(len(cat)) # 必须定义如何度量类长度
# __iter__()
# 必须要返回迭代器
for i in cat:
print(i)
# __getitem__() __setitem__() __delitem__()
print(cat["name"]) # 使类具有下标访问能力
cat["name"] = "Tim"
print(cat["name"]) # 使类具有下标访问能力
print(cat.name)
del cat["name"]
# print(cat.name) 删除了类成员变量 不能打印
# 数学运算 如果未定义函数则报错 如果未定义该加法类型则返回none
# __add__() +
# __sub__() -
# __mul__() *
# __div__() /
# __mod__() %
# __pow__() **
cat_another = Cat("Tom_another", 20)
print(cat + cat_another)
cat2 = cat + cat_another
cat3 = cat_another + cat2
# cat3 = cat2 + cat_another 必须满足顺序传参
print(cat3)
二、类
class Cat:
""" 这是猫类 """
def __init__(self, name, tail_length=10):
self.name = name
self.__private_name = name
self.tail_length = tail_length
print("我是一只猫,我叫%s" % self.name)
def __del__(self):
print("我被系统回收了")
def __call__(self, *args, **kwargs):
print("Cat", args[0]+args[1])
def __str__(self):
return "我是%s" % self.name
def __len__(self):
return self.tail_length
def __iter__(self):
return iter([1, 2, 3, 4])
def __getitem__(self, item):
if item =="name":
return self.name
else:
return None
def __setitem__(self, key, value):
if key == "name":
self.name = value
def __delitem__(self, key):
if key == "name":
del self.name
def __add__(self, other):
if isinstance(other, Cat):
return [self, other]
elif isinstance(other, list):
other.append(self)
return other
版权声明
本文为[qq1033930618]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51326691/article/details/120463976
边栏推荐
猜你喜欢

F. The wonderful use of pad

x86架构初探之8086

Solution of emergency communication system for major security incidents

SDC intelligent communication patrol management system of Nanfang investment building

Typora操作技巧说明(一).md

DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University

免费开源智能充电桩物联网SAAS云平台

GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法

北峰通信助力湛江市消防支队构建PDT无线通信系统

机器视觉系列(01)---综述
随机推荐
How does the public and Private Integrated walkie talkie realize cooperative work under multi-mode communication?
el-date-picker中自定义快捷选项picker-options,动态设置禁用日期
带低压报警的51单片机太阳能充电宝设计与制作(完整代码资料)
可视化之路(十二)Collection类详解
Jupyter Notebook 安装
Draw margin curve in arcface
PyTorch 11. Regularization
自定义钉钉机器人进行报警
Typora操作技巧说明(一)
Systrace parsing
可视化之路(十一)matplotlib颜色详解
jvm知识点汇总-持续更新
How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?
在项目中的定时作用
《Multi-modal Visual Tracking:Review and Experimental Comparison》翻译
基于51单片机的温湿度监测+定时报警系统(c51源码)
记录阿里云服务器挖矿程序处理
el-table 横向滚动条固定在可视窗口底部
《Attention in Natural Language Processing》翻译
可视化常见绘图(三)面积图