当前位置:网站首页>特殊成员与魔法方法
特殊成员与魔法方法
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
边栏推荐
猜你喜欢

无盲区、长续航|公专融合对讲机如何提升酒店服务效率?

Flexible blind patch of ad hoc network | Beifeng oil and gas field survey solution

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

Tensorflow安装后ImportError: DLL load failed: 找不到指定的模块,且国内安装缓慢

Discussion on the outline of short video technology

Int8 quantification and inference of onnx model using TRT

可视化常见绘图(三)面积图

海南凤凰机场智能通信解决方案

AUTOSAR从入门到精通100讲(五十一)-AUTOSAR网络管理

城市应急管理|城市突发事故应急通信指挥调度系统
随机推荐
PyTorch 17. GPU concurrency
【技术规范】:如何写好技术文档?
How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?
学习资料
JDBC连接池
枫桥学院开元名庭酒店DMR系统解决方案
带您遨游太空,美摄科技为航天创意小程序提供全面技术支持
PC端一次启动多个微信
《Multi-modal Visual Tracking:Review and Experimental Comparison》翻译
Hanlp分词器(通过spark)
带低压报警的51单片机太阳能充电宝设计与制作(完整代码资料)
Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
golang实现一个带Web界面的五险一金计算器
自定义classloader并实现热部署-使用loadClass
可视化之路(九)Arrow类详解
自定义钉钉机器人进行报警
【无标题】制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码
Patrol inspection intercom communication system in power industry
可视化常见问题解决方案(七)画图刻度设置解决方案
Beifeng communication helps Zhanjiang fire brigade build PDT wireless communication system