class Company(object):
"""
公司类,用于管理雇员列表
Args:
employee_list (list): 雇员名称列表
"""
def __init__(self, employee_list):
self.employee = employee_list
def __getitem__(self, item):
"""
使Company对象支持索引访问和迭代
Args:
item (int): 索引位置
Returns:
str: 指定索引位置的雇员名称
"""
return self.employee[item]
def __len__(self):
"""
使Company对象支持len()函数
Returns:
int: 雇员数量
"""
return len(self.employee)
def __str__(self):
"""
返回对象的字符串表示形式
Returns:
str: 由employee列表中所有元素用空格连接组成的字符串
"""
return " ".join(self.employee)
def __repr__(self):
"""
返回对象的官方字符串表示形式
Returns:
str: 由employee列表中所有元素用空格连接组成的字符串,用于调试和开发
"""
return " ".join(self.employee)
company = Company(["tom", "bob", "james"])
# 直接访问雇员列表并遍历打印
emploee = company.employee
for i in emploee:
print(i)
# 通过实现__getitem__方法,使Company对象本身支持迭代
# 遍历company容器中的每个元素并打印
for i in company:
print(i)
# 打印company容器的长度
print(len(company))
# 打印整个company容器
print(company)
class Nums(object):
"""
数字类,用于封装数值并支持绝对值运算
Attributes:
num: 数值
"""
def __init__(self, num):
"""
初始化Nums对象
Args:
num: 要存储的数值
"""
self.num = num
def __abs__(self):
"""
计算并返回对象数值的绝对值
Returns:
数值的绝对值
"""
return abs(self.num)
# 创建Nums对象并测试绝对值运算
nu = Nums(-5)
print(abs(nu))
class MyVector(object):
"""
自定义二维向量类,支持基本的数学运算和比较操作
Attributes:
x (int/float): 向量的x坐标分量
y (int/float): 向量的y坐标分量
"""
def __init__(self, x, y):
"""
初始化二维向量
Args:
x (int/float): 向量的x坐标分量
y (int/float): 向量的y坐标分量
"""
self.x = x
self.y = y
def __add__(self, other):
"""
实现向量加法运算
Args:
other (MyVector): 另一个向量对象
Returns:
MyVector: 两个向量相加后的新向量
"""
return MyVector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
"""
实现向量减法运算
Args:
other (MyVector): 另一个向量对象
Returns:
MyVector: 两个向量相减后的新向量
"""
return MyVector(self.x - other.x, self.y - other.y)
def __mul__(self, other):
"""
实现向量乘法运算(对应分量相乘)
Args:
other (MyVector): 另一个向量对象
Returns:
MyVector: 两个向量对应分量相乘后的新向量
"""
return MyVector(self.x * other.x, self.y * other.y)
def __truediv__(self, other):
"""
实现向量除法运算(对应分量相除)
Args:
other (MyVector): 另一个向量对象
Returns:
MyVector: 两个向量对应分量相除后的新向量
"""
return MyVector(self.x / other.x, self.y / other.y)
def __str__(self):
"""
返回向量的字符串表示形式
Returns:
str: 格式为"(x,y)"的字符串
"""
return "({},{})".format(self.x, self.y)
def __repr__(self):
"""
返回向量的官方字符串表示形式
Returns:
str: 格式为"(x,y)"的字符串
"""
return "({},{})".format(self.x, self.y)
def __eq__(self, other):
"""
实现向量相等比较运算
Args:
other (MyVector): 另一个向量对象
Returns:
bool: 如果两个向量的x和y分量都相等则返回True,否则返回False
"""
return self.x == other.x and self.y == other.y
def __ne__(self, other):
"""
实现向量不等比较运算
Args:
other (MyVector): 另一个向量对象
Returns:
bool: 如果两个向量的任一分量不相等则返回True,否则返回False
"""
return self.x != other.x or self.y != other.y
def __lt__(self, other):
"""
实现向量小于比较运算(两个分量都小于)
Args:
other (MyVector): 另一个向量对象
Returns:
bool: 如果当前向量的x和y分量都小于另一个向量则返回True,否则返回False
"""
return self.x < other.x and self.y < other.y
def __le__(self, other):
"""
实现向量小于等于比较运算(两个分量都小于等于)
Args:
other (MyVector): 另一个向量对象
Returns:
bool: 如果当前向量的x和y分量都小于等于另一个向量则返回True,否则返回False
"""
return self.x <= other.x and self.y <= other.y
def __gt__(self, other):
"""
实现向量大于比较运算(两个分量都大于)
Args:
other (MyVector): 另一个向量对象
Returns:
bool: 如果当前向量的x和y分量都大于另一个向量则返回True,否则返回False
"""
return self.x > other.x and self.y > other.y
def __ge__(self, other):
"""
实现向量大于等于比较运算(两个分量都大于等于)
Args:
other (MyVector): 另一个向量对象
Returns:
bool: 如果当前向量的x和y分量都大于等于另一个向量则返回True,否则返回False
"""
return self.x >= other.x and self.y >= other.y
def __abs__(self):
"""
实现向量绝对值运算(曼哈顿距离)
Returns:
int/float: 向量各分量绝对值之和
"""
return abs(self.x) + abs(self.y)
def __len__(self):
"""
实现向量长度运算(字符串表示的长度)
Returns:
int: 向量字符串表示的字符个数
"""
return len(str(self))
def __getitem__(self, item):
"""
实现向量索引访问功能
Args:
item (int): 索引值,0表示x分量,1表示y分量
Returns:
int/float: 对应索引位置的分量值
Raises:
IndexError: 当索引值不是0或1时抛出异常
"""
if item == 0:
return self.x
elif item == 1:
return self.y
else:
raise IndexError("Vector index out of range")
# 创建两个测试向量
f_vec = MyVector(1, 2)
s_vec = MyVector(3, 4)
# 测试各种向量运算和操作
print(f_vec + s_vec) # 向量加法
print(f_vec - s_vec) # 向量减法
print(f_vec * s_vec) # 向量乘法
print(f_vec / s_vec) # 向量除法
print(abs(f_vec)) # 向量绝对值
print(len(f_vec)) # 向量长度
print(f_vec[0]) # 访问x分量
print(f_vec[1]) # 访问y分量
print(f_vec == s_vec) # 向量相等比较
print(f_vec != s_vec) # 向量不等比较
print(f_vec < s_vec) # 向量小于比较
print(f_vec <= s_vec) # 向量小于等于比较
print(f_vec > s_vec) # 向量大于比较
print(f_vec >= s_vec) # 向量大于等于比较
print(repr(f_vec)) # 向量repr表示
print(str(f_vec)) # 向量str表示
print(f_vec) # 向量默认输出
tom
bob
james
tom
bob
james
3
tom bob james
5
(4,6)
(-2,-2)
(3,8)
(0.3333333333333333,0.5)
3
5
1
2
False
True
True
True
False
False
(1,2)
(1,2)
(1,2)