当前位置:网站首页>Special members and magic methods
Special members and magic methods
2022-04-23 10:47:00 【qq1033930618】
List of articles
One 、 The main function
from module_a import Cat
# __init__() Constructors
cat = Cat("Tom", 15)
# # __del__() Destructor Call explicitly or automatically at the end of the program
# del cat
# # print(cat) Not printable
# __doc__ __modules__ __class__
print(cat.__doc__) # Print class description document
print(cat.__module__) # Print the module name of the class
print(cat.__class__) # Print the location of the class
# __call__()
cat(1, 2) # Make the class be called like a function
print(callable(cat))
# __dict__
print(cat.__dict__) # Print common and private member properties in dictionary form
print(cat._Cat__private_name) # Access private member properties
# __str__()
print(cat) # If the print object returns a property Not the access address
print(str(cat))
# __len__()
print(len(cat)) # You must define how to measure class length
# __iter__()
# Must return iterators
for i in cat:
print(i)
# __getitem__() __setitem__() __delitem__()
print(cat["name"]) # Make the class subscript accessible
cat["name"] = "Tim"
print(cat["name"]) # Make the class subscript accessible
print(cat.name)
del cat["name"]
# print(cat.name) Deleted class member variable Can't print
# Mathematical operations If no function is defined, an error is reported If the addition type is not defined, return 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 The order of transmission must be met
print(cat3)
Two 、 class
class Cat:
""" This is a cat """
def __init__(self, name, tail_length=10):
self.name = name
self.__private_name = name
self.tail_length = tail_length
print(" I'm a cat , My name is %s" % self.name)
def __del__(self):
print(" I was recycled by the system ")
def __call__(self, *args, **kwargs):
print("Cat", args[0]+args[1])
def __str__(self):
return " I am a %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://yzsam.com/2022/04/202204230618497283.html
边栏推荐
- Solution architect's small bag - 5 types of architecture diagrams
- MySQL common statements
- 997、有序数组的平方(数组)
- 【leetcode】107. Sequence traversal of binary tree II
- JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
- IDEA——》每次启动都会Indexing或 scanning files to index
- Arm debugging (1): two methods to redirect printf to serial port in keil
- UDP basic learning
- Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
- 202、快乐数
猜你喜欢
Comparison and practice of prototype design of knowledge service app
Example of pop-up task progress bar function based on pyqt5
SQL Server cursor circular table data
Xshell+Xftp 下载安装步骤
/Can etc / shadow be cracked?
Sim Api User Guide(5)
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
高价买来的课程,公开了!phper资料分享
部署jar包
Comparison and practice of prototype design of knowledge service app
随机推荐
Jerry's factors that usually affect CPU performance test results are: [article]
UDP basic learning
【leetcode】107.二叉树的层序遍历II
Problems of class in C # and database connection
Xshell+Xftp 下载安装步骤
Reading integrity monitoring techniques for vision navigation systems - 5 Results
Leetcode22:括号生成
MySQL common statements
Example of pop-up task progress bar function based on pyqt5
JVM——》常用参数
Sim Api User Guide(7)
全栈交叉编译X86完成过程经验分享
Reading integrity monitoring techniques for vision navigation systems - 3 background
Read integrity monitoring techniques for vision navigation systems
MapReduce core and foundation demo
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
142. Circular linked list||
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
景联文科技—专业数据标注公司和智能数据标注平台
Detailed explanation of MapReduce calculation process