Python魔法之旅-魔法方法(14)

目录

一、概述

1、定义

2、作用

二、应用场景

1、构造和析构

2、操作符重载

3、字符串和表示

4、容器管理

5、可调用对象

6、上下文管理

7、属性访问和描述符

8、迭代器和生成器

9、数值类型

10、复制和序列化

11、自定义元类行为

12、自定义类行为

13、类型检查和转换

14、自定义异常

三、学习方法

1、理解基础

2、查阅文档

3、编写示例

4、实践应用

5、阅读他人代码

6、参加社区讨论

7、持续学习

8、练习与总结

9、注意兼容性

10、避免过度使用

四、魔法方法

44、__length_hint__方法

44-1、语法

44-2、参数

44-3、功能

44-4、返回值

44-5、说明

44-6、用法

45、__lshift__方法

45-1、语法

45-2、参数

45-3、功能

45-4、返回值

45-5、说明

45-6、用法

46、__lt__方法

46-1、语法

46-2、参数

46-3、功能

46-4、返回值

46-5、说明

46-6、用法

五、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、博客个人主页

一、概述

1、定义

魔法方法**(Magic Methods/Special Methods,也称特殊方法或双下划线方法)是Python中一类具有特殊命名规则的方法,它们的名称通常以双下划线(`__`)开头和结尾**。

魔法方法用于在特定情况下**自动被Python解释器调用,而不需要显式地调用它们,它们提供了一种机制,**让你可以定义自定义类时具有与内置类型相似的行为。

2、作用

魔法方法允许开发者重载Python中的一些内置操作或函数的行为,从而为自定义的类添加特殊的功能

二、应用场景

1、构造和析构

1-1、init(self, [args...]):在创建对象时初始化属性。

1-2、new(cls, [args...]):在创建对象时控制实例的创建过程(通常与元类一起使用)。

1-3、del(self):在对象被销毁前执行清理操作,如关闭文件或释放资源。

2、操作符重载

2-1、add(self, other)、sub(self, other)、mul(self, other)等:自定义对象之间的算术运算。

2-2、eq(self, other)、ne(self, other)、lt(self, other)等:定义对象之间的比较操作。

3、字符串和表示

3-1、str(self):定义对象的字符串表示,常用于print()函数。

3-2、repr(self):定义对象的官方字符串表示,用于repr()函数和交互式解释器。

4、容器管理

4-1、getitem(self, key)、setitem(self, key, value)、delitem(self, key):用于实现类似列表或字典的索引访问、设置和删除操作。

4-2、len(self):返回对象的长度或元素个数。

5、可调用对象

5-1、call(self, [args...]):允许对象像函数一样被调用。

6、上下文管理

6-1、enter(self)、exit(self, exc_type, exc_val, exc_tb):用于实现上下文管理器,如with语句中的对象。

7、属性访问和描述符

7-1、getattr, setattr, delattr:这些方法允许对象在访问或修改不存在的属性时执行自定义操作。

7-2、描述符(Descriptors)是实现了__get__, set, 和__delete__方法的对象,它们可以控制对另一个对象属性的访问。

8、迭代器和生成器

8-1、iter__和__next:这些方法允许对象支持迭代操作,如使用for循环遍历对象。

8-2、aiter, anext:这些是异步迭代器的魔法方法,用于支持异步迭代。

9、数值类型

9-1、int(self)、float(self)、complex(self):定义对象到数值类型的转换。

9-2、index(self):定义对象用于切片时的整数转换。

10、复制和序列化

10-1、copy__和__deepcopy:允许对象支持浅复制和深复制操作。

10-2、getstate__和__setstate:用于自定义对象的序列化和反序列化过程。

11、自定义元类行为

11-1、metaclass(Python 2)或元类本身(Python 3):允许自定义类的创建过程,如动态创建类、修改类的定义等。

12、自定义类行为

12-1、init__和__new:用于初始化对象或控制对象的创建过程。

12-2、init_subclass:在子类被创建时调用,允许在子类中执行一些额外的操作。

13、类型检查和转换

13-1、instancecheck__和__subclasscheck:用于自定义isinstance()和issubclass()函数的行为。

14、自定义异常

14-1、你可以通过继承内置的Exception类来创建自定义的异常类,并定义其特定的行为。

三、学习方法

要学好Python的魔法方法,你可以遵循以下方法及步骤:

1、理解基础

首先确保你对Python的基本语法、数据类型、类和对象等概念有深入的理解,这些是理解魔法方法的基础。

2、查阅文档

仔细阅读Python官方文档中关于魔法方法的部分,文档会详细解释每个魔法方法的作用、参数和返回值。你可以通过访问Python的官方网站或使用help()函数在Python解释器中查看文档。

3、编写示例

为每个魔法方法编写简单的示例代码,以便更好地理解其用法和效果,通过实际编写和运行代码,你可以更直观地感受到魔法方法如何改变对象的行为。

4、实践应用

在实际项目中尝试使用魔法方法。如,你可以创建一个自定义的集合类,使用__getitem__、__setitem__和__delitem__方法来实现索引操作。只有通过实践应用,你才能更深入地理解魔法方法的用途和重要性。

5、阅读他人代码

阅读开源项目或他人编写的代码,特别是那些使用了魔法方法的代码,这可以帮助你学习如何在实际项目中使用魔法方法。通过分析他人代码中的魔法方法使用方式,你可以学习到一些新的技巧和最佳实践。

6、参加社区讨论

参与Python社区的讨论,与其他开发者交流关于魔法方法的使用经验和技巧,在社区中提问或回答关于魔法方法的问题,这可以帮助你更深入地理解魔法方法并发现新的应用场景。

7、持续学习

Python语言和其生态系统不断发展,新的魔法方法和功能可能会不断被引入,保持对Python社区的关注,及时学习新的魔法方法和最佳实践。

8、练习与总结

多做练习,通过编写各种使用魔法方法的代码来巩固你的理解,定期总结你学到的知识和经验,形成自己的知识体系。

9、注意兼容性

在使用魔法方法时,要注意不同Python版本之间的兼容性差异,确保你的代码在不同版本的Python中都能正常工作。

10、避免过度使用

虽然魔法方法非常强大,但过度使用可能会导致代码难以理解和维护,在编写代码时,要权衡使用魔法方法的利弊,避免滥用。

总之,学好Python的魔法方法需要不断地学习、实践和总结,只有通过不断地练习和积累经验,你才能更好地掌握这些强大的工具,并在实际项目中灵活运用它们。

四、魔法方法

44、__length_hint__方法

44-1、语法
python 复制代码
__length_hint__(self, /)
    Private method returning an estimate of len(list(it))
44-2、参数

44-2-1、self**(必须)****:**一个对实例对象本身的引用,在类的所有方法中都会自动传递。

**44-2-2、/(可选):**这是从Python 3.8开始引入的参数注解语法,它表示这个方法不接受任何位置参数(positional-only parameters)之后的关键字参数(keyword arguments)。

44-3、功能

为那些需要知道迭代器大致长度的函数或方法提供一个长度提示。

44-4、返回值

返回一个整数或None。

44-5、说明

length_hint方法应该返回一个整数或None

44-5-1、如果返回整数,它应该是一个对迭代器长度的合理估计,这个估计可能并不总是准确的,但它应该尽可能接近实际长度。

44-5-2、如果迭代器长度未知或无法合理估计,则应返回None

44-6、用法
python 复制代码
# 044、__length_hint__方法:
# 1、简单的列表包装器
class MyListWrapper:
    def __init__(self, lst):
        self.lst = lst
    def __length_hint__(self):
        return len(self.lst)

# 2、迭代器长度提示
class MyIterator:
    def __init__(self, numbers):
        self.numbers = numbers
        self.index = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index < len(self.numbers):
            result = self.numbers[self.index]
            self.index += 1
            return result
        raise StopIteration
    def __length_hint__(self):
        return len(self.numbers) - self.index

# 3、动态生成的序列
class DynamicSequence:
    def __init__(self, start, end):
        self.start = start
        self.end = end
    def __iter__(self):
        return self
    def __next__(self):
        if self.start > self.end:
            raise StopIteration
        current = self.start
        self.start += 1
        return current
    def __length_hint__(self):
        return self.end - self.start + 1 if self.start <= self.end else 0

# 4、缓存长度的迭代器
class CachedLengthIterator:
    def __init__(self, iterable):
        self.iterable = iterable
        self._length = None
    def __iter__(self):
        return iter(self.iterable)
    def __length_hint__(self):
        if self._length is None:
            self._length = sum(1 for _ in self.iterable)
        return self._length

# 5、生成器长度提示(通常不准确,因为生成器是动态的)
def my_generator(n):
    for i in range(n):
        yield i
class GeneratorWrapper:
    def __init__(self, generator_func, *args, **kwargs):
        self.generator = generator_func(*args, **kwargs)
        self._hint = None
    def __iter__(self):
        return iter(self.generator)
    def __length_hint__(self):
        if self._hint is None:
            # 注意:这只是一个示例,对于真正的生成器可能不准确
            self._hint = sum(1 for _ in self.generator)
        return self._hint
        # 使用示例:GeneratorWrapper(my_generator, 10)

# 6、模拟数据库查询结果
class DatabaseQueryResult:
    def __init__(self, count):
        self.count = count
    def __length_hint__(self):
        return self.count

# 7、文件行数预估(基于文件大小)
class FileLineCounterApprox:
    def __init__(self, file_path, avg_line_length=100):  # 假设平均行长为100字节
        self.file_path = file_path
        self.avg_line_length = avg_line_length
    def __length_hint__(self):
        with open(self.file_path, 'rb') as file:
            return file.seek(0, 2) // self.avg_line_length  # 粗略估计

# 8、树形结构节点数预估
class TreeNode:
    def __init__(self, value, children=None):
        self.value = value
        self.children = children if children is not None else []
    def __length_hint__(self):
        # 假设这是一个完全二叉树,用于示例
        return (2 ** self.depth()) - 1 if self.is_full_binary_tree() else "unknown"
    def depth(self):
        # 需要实现一个计算树深度的函数
        pass
    def is_full_binary_tree(self):
        # 需要实现一个检查树是否是完全二叉树的函数
        pass
        # 注意:在实际应用中,树的长度(即节点数)可能需要递归遍历才能准确计算

# 9、网络请求结果集大小预估
class APIResult:
    def __init__(self, response_headers):
        self.response_headers = response_headers
    def __length_hint__(self):
        # 假设API响应头中包含了一个关于结果集大小的字段
        content_length = self.response_headers.get('X-Result-Set-Size', None)
        return int(content_length) if content_length is not None and content_length.isdigit() else "unknown"
        # 注意:这个预估是基于假设的HTTP头字段,实际API可能不会有这样的字段

# 10、流式数据处理长度提示
class StreamProcessor:
    def __init__(self, stream):
        self.stream = stream
        self._processed_count = 0
    def process(self, chunk_size=1024):
        # 处理流数据,这里只是模拟
        for _ in range(chunk_size):
            # 假设每次处理一个数据单元
            self._processed_count += 1
    def __length_hint__(self):
        # 流数据长度通常未知,但可以提供已处理的数量
        return self._processed_count
        # 注意:流数据的长度通常是未知的,除非有额外的信息或上下文

# 11、自定义集合大小预估
class CustomSet:
    def __init__(self, iterable=()):
        self._set = set(iterable)
        self._hint = None
    def add(self, item):
        self._set.add(item)
        self._hint = None  # 清除长度提示缓存
    def remove(self, item):
        self._set.remove(item)
        self._hint = None
    def __iter__(self):
        return iter(self._set)
    def __len__(self):
        return len(self._set)
    def __length_hint__(self):
        if self._hint is None:
            self._hint = len(self)  # 只有在需要时才计算长度
        return self._hint
        # 注意:在实际应用中,集合的大小可以直接通过len()函数获取,但这里展示了如何使用__length_hint__进行缓存

45、__lshift__方法

45-1、语法
python 复制代码
__lshift__(self, other, /)
    Return self << other
45-2、参数

45-2-1、self**(必须)****:**一个对实例对象本身的引用,在类的所有方法中都会自动传递。

45-2-2、 other**(必须)****:**表示与self进行左移操作的另一个对象或整数。

**45-2-3、/(可选):**这是从Python 3.8开始引入的参数注解语法,它表示这个方法不接受任何位置参数(positional-only parameters)之后的关键字参数(keyword arguments)。

45-3、功能

用于定义对象与左移位运算符<<的行为。

45-4、返回值

返回一个与self类型相同或兼容的对象,该对象表示self对象左移other位之后的结果。

45-5、说明

如果类没有定义__lshift__方法,并且尝试使用左移位运算符<<对其实例进行操作,将会引发一个TypeError。

45-6、用法
python 复制代码
# 045、__lshift__方法:
# 1、二进制位移
class BinaryShift:
    def __init__(self, value):
        self.value = value
    def __lshift__(self, other):
        return BinaryShift(self.value << other)
if __name__ == '__main__':
    a = BinaryShift(4)
    b = a << 2  # 相当于 4 << 2 = 16
    print(b.value)  # 输出 16

# 2、整数列表左移
class IntList:
    def __init__(self, values):
        self.values = values
    def __lshift__(self, other):
        if other < 0 or other >= len(self.values):
            raise ValueError("Invalid shift amount")
        return IntList(self.values[other:] + self.values[:other])
if __name__ == '__main__':
    lst = IntList([3, 5, 6, 8, 10, 11, 24])
    shifted = lst << 2  # 相当于 [6, 8, 10, 11, 24, 3, 5]
    print(shifted.values)  # 输出 [6, 8, 10, 11, 24, 3, 5]

# 3、字符串左移
class StringShift:
    def __init__(self, value):
        self.value = value
    def __lshift__(self, other):
        if other < 0:
            return StringShift(self.value[-other:] + self.value[:-other])
        elif other >= len(self.value):
            return StringShift(self.value)
        else:
            return StringShift(self.value[other:] + self.value[:other])
if __name__ == '__main__':
    s = StringShift("hello")
    shifted = s << 2  # 相当于 "llohe"
    print(shifted.value)  # 输出 "llohe"

# 4、时间戳左移(模拟时间偏移)
from datetime import datetime, timedelta
class TimestampShift:
    def __init__(self, timestamp):
        self.timestamp = timestamp
    def __lshift__(self, other):
        # 假设 other 是天数
        return TimestampShift(self.timestamp + timedelta(days=other))
if __name__ == '__main__':
    now = datetime.now()
    ts = TimestampShift(now)
    future = ts << 10  # 10天后的时间
    print(future.timestamp)  # 输出10天后的日期时间

# 5、图形对象的水平移动
class Shape:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __lshift__(self, other):
        return Shape(self.x + other, self.y)
if __name__ == '__main__':
    rect = Shape(10, 24)
    moved = rect << 5  # 向右移动5个单位
    print(moved.x, moved.y)  # 输出 15 24

# 6、权重调整(模拟权重左移)
class WeightedItem:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight
    def __lshift__(self, other):
        # 假设 other 是权重增加的因子(例如,左移1位相当于权重乘以2)
        return WeightedItem(self.value, self.weight * (2 ** other))
if __name__ == '__main__':
    item = WeightedItem('apple', 1)
    item_with_higher_weight = item << 1  # 权重变为 2
    print(item_with_higher_weight.weight)  # 输出 2

# 7、颜色深度调整(模拟颜色通道值的左移)
class Color:
    def __init__(self, r, g, b):
        self.r = r
        self.g = g
        self.b = b
    def __lshift__(self, other):
        # 假设 other 是亮度增加的因子(注意:这里需要确保值在有效范围内)
        return Color(min(self.r << other, 255), min(self.g << other, 255), min(self.b << other, 255))
if __name__ == '__main__':
    red = Color(255, 0, 0)
    brighter_red = red << 1  # 增加亮度
    print(brighter_red.r, brighter_red.g, brighter_red.b)  # 输出:255 0 0

# 8、频率调整(模拟音频信号频率的左移)
class AudioSignal:
    # 这里仅作为示例,实际音频处理会更复杂
    def __init__(self, frequencies):
        self.frequencies = frequencies
    def __lshift__(self, other):
        # 假设 other 是频率增加的因子(这在实际中是不准确的,仅作示例)
        return AudioSignal([freq * (2 ** other) for freq in self.frequencies])
if __name__ == '__main__':
    signal = AudioSignal([100, 200, 300])
    higher_freq_signal = signal << 1  # 模拟频率加倍
    print(higher_freq_signal.frequencies)  # 输出:[200, 400, 600]

# 9、队列元素左移(删除头部元素并在尾部添加新元素)
class Queue:
    def __init__(self):
        self.items = []
    def enqueue(self, item):
        self.items.append(item)
    def dequeue(self):
        if self.is_empty():
            raise IndexError("Queue is empty")
        return self.items.pop(0)
    def is_empty(self):
        return len(self.items) == 0
    # 模拟左移(删除头部并添加新元素)
    def __lshift__(self, new_item):
        self.dequeue()
        self.enqueue(new_item)
if __name__ == '__main__':
    q = Queue()
    q.enqueue(1)
    q.enqueue(2)
    q.enqueue(3)
    print(q.items)  # 输出 [1, 2, 3]
    q << 4  # 删除1并添加4
    print(q.items)  # 输出 [2, 3, 4]

# 10、价格调整(模拟折扣)
class Price:
    def __init__(self, value):
        self.value = value
    def __lshift__(self, discount_factor):
        # 假设 discount_factor 是折扣的百分比(例如,左移1位代表打五折)
        # 注意:这里的实现仅作为示例,真实的折扣计算会更复杂
        return Price(self.value * (1 - discount_factor / 100))
if __name__ == '__main__':
    original_price = Price(100)
    discounted_price = original_price << 50  # 假设代表打五折
    print(discounted_price.value)  # 输出 50.0

# 11、时间线事件的前移
class TimelineEvent:
    def __init__(self, name, timestamp):
        self.name = name
        self.timestamp = timestamp
    def __lshift__(self, time_delta):
        # 将事件前移指定的时间量
        self.timestamp -= time_delta
if __name__ == '__main__':
    event = TimelineEvent('Meeting', datetime.now())
    print(event.timestamp)  # 输出当前时间
    event << timedelta(hours=1)  # 将事件前移1小时
    print(event.timestamp)  # 输出前移1小时后的时间

# 12、图形界面中的元素左移
class GUIElement:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __lshift__(self, shift_amount):
        # 将元素向左移动指定的像素量
        self.x = max(0, self.x - shift_amount)  # 确保元素不会移出容器的左侧边界
    def __repr__(self):
        # 为了更友好地打印GUIElement对象
        return f"GUIElement(x={self.x}, y={self.y})"
# 假设的GUI容器类(仅用于演示)
class GUIContainer:
    def __init__(self):
        self.elements = []
    def add_element(self, element):
        # 添加元素到容器中(这里只是简单地将元素添加到列表中)
        self.elements.append(element)
# 主程序
if __name__ == '__main__':
    # 创建一个GUI容器
    container = GUIContainer()
    # 创建一个GUI元素(按钮)并添加到容器中(虽然在这里我们并不真正添加到GUI中)
    button = GUIElement(100, 50)
    # 假设有一个add_element方法添加到容器中(这里我们手动调用)
    container.add_element(button)
    # 将按钮向左移动20像素
    button << 20  # 调用__lshift__方法
    # 打印按钮的新位置
    print(button)  # 输出类似:GUIElement(x=80, y=50)

46、__lt__方法

46-1、语法
python 复制代码
__lt__(self, other, /)
    Return self < other
46-2、参数

46-2-1、self**(必须)****:**一个对实例对象本身的引用,在类的所有方法中都会自动传递。

46-2-2、 other**(必须)****:**表示与self进行比较的另一个对象。

**46-2-3、/(可选):**这是从Python 3.8开始引入的参数注解语法,它表示这个方法不接受任何位置参数(positional-only parameters)之后的关键字参数(keyword arguments)。

46-3、功能

用于定义对象之间"小于"关系的行为。

46-4、返回值

返回一个布尔值(True或False),表示self是否小于other。

46-5、说明

如果没有在类中定义__lt__方法,并且尝试使用<运算符对其实例进行比较,将会引发一个TypeError,除非该类的实例是某个内置类型的子类(如int、float、str等),这些内置类型已经定义了它们自己的__lt__方法。

46-6、用法
python 复制代码
# 046、__lt__方法:
# 1、自定义整数类
class MyInt:
    def __init__(self, value):
        self.value = value
    def __lt__(self, other):
        if isinstance(other, MyInt):
            return self.value < other.value
        elif isinstance(other, int):
            return self.value < other
        else:
            raise TypeError("Unsupported operand types for <: 'MyInt' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    a = MyInt(5)
    b = MyInt(10)
    print(a < b)  # True

# 2、自定义字符串长度比较
class StringLength:
    def __init__(self, s):
        self.s = s
    def __lt__(self, other):
        if isinstance(other, StringLength):
            return len(self.s) < len(other.s)
        elif isinstance(other, int):
            return len(self.s) < other
        else:
            raise TypeError("Unsupported operand types for <: 'StringLength' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    s1 = StringLength("apple")
    s2 = StringLength("banana")
    print(s1 < s2)  # True

# 3、自定义日期类
from datetime import date
class MyDate:
    def __init__(self, year, month, day):
        self.date = date(year, month, day)
    def __lt__(self, other):
        if isinstance(other, MyDate):
            return self.date < other.date
        else:
            raise TypeError("Unsupported operand types for <: 'MyDate' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    d1 = MyDate(2024, 3, 13)
    d2 = MyDate(2024, 6, 4)
    print(d1 < d2)  # True

# 4、自定义二维点类
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __lt__(self, other):
        if isinstance(other, Point):
            return (self.x, self.y) < (other.x, other.y)
        else:
            raise TypeError("Unsupported operand types for <: 'Point' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    p1 = Point(3, 6)
    p2 = Point(5, 11)
    print(p1 < p2)  # True

# 5、自定义矩形面积比较
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    @property
    def area(self):
        return self.width * self.height
    def __lt__(self, other):
        if isinstance(other, Rectangle):
            return self.area < other.area
        else:
            raise TypeError("Unsupported operand types for <: 'Rectangle' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    rect1 = Rectangle(3, 6)
    rect2 = Rectangle(5, 11)
    print(rect1 < rect2)  # True

# 6、自定义字典按值排序比较(仅比较第一个值)
class ValueSortedDict:
    def __init__(self, dict_items):
        self.dict_items = sorted(dict_items.items(), key=lambda x: x[1])
    def __lt__(self, other):
        if isinstance(other, ValueSortedDict):
            return self.dict_items[0][1] < other.dict_items[0][1]
        else:
            raise TypeError("Unsupported operand types for <: 'ValueSortedDict' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    dict1 = {'a': 10, 'b': 24}
    dict2 = {'c': 10, 'd': 8}
    vsd1 = ValueSortedDict(dict1)
    vsd2 = ValueSortedDict(dict2)
    print(vsd1 < vsd2)  # False

# 7、自定义分数类,按分数值比较
from fractions import Fraction
class FractionWrapper:
    def __init__(self, numerator, denominator):
        self.fraction = Fraction(numerator, denominator)
    def __lt__(self, other):
        if isinstance(other, FractionWrapper):
            return self.fraction < other.fraction
        elif isinstance(other, Fraction):
            return self.fraction < other
        else:
            raise TypeError("Unsupported operand types for <: 'FractionWrapper' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    frac1 = FractionWrapper(10, 24)
    frac2 = FractionWrapper(3, 6)
    print(frac1 < frac2)  # True

# 8、自定义复数类,按模长比较
import math
class ComplexNumber:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
    @property
    def modulus(self):
        return math.sqrt(self.real ** 2 + self.imag ** 2)
    def __lt__(self, other):
        if isinstance(other, ComplexNumber):
            return self.modulus < other.modulus
        else:
            raise TypeError("Unsupported operand types for <: 'ComplexNumber' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    c1 = ComplexNumber(3, 6)
    c2 = ComplexNumber(5, 11)
    print(c1 < c2)  # True

# 9、自定义时间戳类,按时间先后比较
from datetime import datetime, timezone
class Timestamp:
    def __init__(self, timestamp):
        self.timestamp = datetime.fromtimestamp(timestamp, tz=timezone.utc)
    def __lt__(self, other):
        if isinstance(other, Timestamp):
            return self.timestamp < other.timestamp
        elif isinstance(other, datetime):
            return self.timestamp < other
        else:
            raise TypeError("Unsupported operand types for <: 'Timestamp' and '{}'".format(type(other).__name__))
if __name__ == '__main__':
    ts1 = Timestamp(1609459200)  # 2021-01-01 00:00:00 UTC
    ts2 = Timestamp(1609545600)  # 2021-01-02 00:00:00 UTC
    print(ts1 < ts2)  # True

# 10、自定义二维点类,按字典序比较
class Point2D:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __lt__(self, other):
        if not isinstance(other, Point2D):
            return NotImplemented
        if self.x < other.x:
            return True
        elif self.x == other.x and self.y < other.y:
            return True
        return False
    def __repr__(self):
        return f"Point2D({self.x}, {self.y})"
if __name__ == '__main__':
    p1 = Point2D(1, 2)
    p2 = Point2D(2, 1)
    p3 = Point2D(1, 3)
    print(p1 < p2)  # True
    print(p1 < p3)  # True
    print(p2 < p3)  # False

# 11、自定义图书类,按价格或出版时间比较
from datetime import datetime
class Book:
    def __init__(self, title, author, price, publish_date):
        self.title = title
        self.author = author
        self.price = price
        self.publish_date = datetime.strptime(publish_date, '%Y-%m-%d')
    def __repr__(self):
        return f"Book(title={self.title}, author={self.author}, price={self.price}, publish_date={self.publish_date.strftime('%Y-%m-%d')})"
    def __lt_by_price__(self, other):
        if not isinstance(other, Book):
            return NotImplemented
        return self.price < other.price
    def __lt_by_publish_date__(self, other):
        if not isinstance(other, Book):
            return NotImplemented
        return self.publish_date < other.publish_date
if __name__ == '__main__':
    book1 = Book("Python Programming", "Alice", 39.99, "2022-01-01")
    book2 = Book("Java for Beginners", "Bob", 29.99, "2021-05-15")
    book3 = Book("Database Management", "Charlie", 49.99, "2022-06-30")
    # 按价格比较
    print(book1.__lt_by_price__(book2))  # True,因为book1的价格比book2高
    print(book2.__lt_by_price__(book3))  # True,因为book2的价格比book3低
    # 按出版时间比较
    print(book1.__lt_by_publish_date__(book2))  # False,因为book1的出版时间比book2晚
    print(book2.__lt_by_publish_date__(book3))  # True,因为book2的出版时间比book3早

# 12、自定义学生类,按成绩比较
class Student:
    def __init__(self, name, score):
        self.name = name
        self.score = score
    def __repr__(self):
        return f"Student(name={self.name}, score={self.score})"
    def __lt__(self, other):
        if not isinstance(other, Student):
            return NotImplemented
        return self.score < other.score
if __name__ == '__main__':
    student1 = Student("Myelsa", 85)
    student2 = Student("Bruce", 90)
    student3 = Student("Jimmy", 85)
    # 按成绩比较
    print(student1 < student2)  # True,因为student1的成绩比student2低
    print(student1 < student3)  # False,因为student1和student3的成绩相同
    print(student2 < student3)  # False,因为student2的成绩比student3高

五、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、博客个人主页

相关推荐
海盗猫鸥16 分钟前
C++入门基础篇(1)
开发语言·c++·学习
专注成就自我24 分钟前
java使用easypoi模版导出word详细步骤
java·开发语言·word
.生产的驴1 小时前
SpringBoot AOP切入点表达式
spring boot·后端·python
多多*1 小时前
SpringBoot 启动流程六
java·开发语言·spring boot·后端·spring
让你三行代码QAQ1 小时前
SpringSecurity初始化过程
java·开发语言
逆水寻舟1 小时前
算法学习记录2
python·学习·算法
Mr_Richard1 小时前
Java动态代理的实现方式
java·开发语言
B站计算机毕业设计超人2 小时前
计算机毕业设计Python深度学习美食推荐系统 美食可视化 美食数据分析大屏 美食爬虫 美团爬虫 机器学习 大数据毕业设计 Django Vue.js
大数据·python·深度学习·机器学习·数据分析·课程设计·推荐算法
码农超哥同学2 小时前
Python面试题:请解释 `lambda` 函数是什么,并举一个例子
开发语言·python·面试·编程
sssjjww2 小时前
python输出日志out.log相关问题(缓存机制)
java·python·缓存