From 4b7f64909dc2a82ca6dcea7e4ae866f19029b261 Mon Sep 17 00:00:00 2001 From: Samzong Date: Sat, 27 Jul 2024 20:33:48 +0800 Subject: [PATCH] Create 2024-07-27-python-advanced-usage.md --- blog/2024-07-27-python-advanced-usage.md | 119 +++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 blog/2024-07-27-python-advanced-usage.md diff --git a/blog/2024-07-27-python-advanced-usage.md b/blog/2024-07-27-python-advanced-usage.md new file mode 100644 index 000000000..7c020eeac --- /dev/null +++ b/blog/2024-07-27-python-advanced-usage.md @@ -0,0 +1,119 @@ +--- +title: Python 常见的高阶用法 +tags: [Python] +--- + +Python 的一些高阶用法和特性,以下是一些常见的高阶用法: + +1. **生成器(Generators)**: + 生成器是一种特殊的迭代器,可以用来生成一系列值。使用 `yield` 关键字创建生成器函数。 + + ```python + def my_generator(): + yield 1 + yield 2 + yield 3 + + gen = my_generator() + for value in gen: + print(value) + ``` + +2. **上下文管理器(Context Managers)**: + 使用 `with` 语句和 `contextlib` 模块来管理资源,如文件、网络连接等。 + + ```python + with open('example.txt', 'r') as file: + content = file.read() + ``` + +3. **装饰器(Decorators)**: + 装饰器是一种高阶函数,可以在不修改原函数代码的情况下,对其进行扩展或修改。 + + ```python + def my_decorator(func): + def wrapper(): + print("Something is happening before the function is called.") + func() + print("Something is happening after the function is called.") + return wrapper + + @my_decorator + def say_hello(): + print("Hello!") + + say_hello() + ``` + +4. **类装饰器(Class Decorators)**: + 类装饰器可以用来扩展或修改类的行为。 + + ```python + def class_decorator(cls): + cls.new_attribute = "This is a new attribute" + return cls + + @class_decorator + class MyClass: + pass + + print(MyClass.new_attribute) + ``` + +5. **元类(Metaclasses)**: + 元类是创建类的类。可以用来自定义类的创建行为。 + + ```python + class Meta(type): + def __new__(cls, name, bases, dct): + dct['new_attribute'] = "This is a new attribute" + return super().__new__(cls, name, bases, dct) + + class MyClass(metaclass=Meta): + pass + + print(MyClass.new_attribute) + ``` + +6. **函数式编程(Functional Programming)**: + 使用 `map`、`filter`、`reduce` 等高阶函数进行函数式编程。 + + ```python + from functools import reduce + + numbers = [1, 2, 3, 4] + squared = list(map(lambda x: x ** 2, numbers)) + filtered = list(filter(lambda x: x % 2 == 0, numbers)) + summed = reduce(lambda x, y: x + y, numbers) + + print(squared) + print(filtered) + print(summed) + ``` + +7. **协程(Coroutines)**: + 协程是一种用于异步编程的工具,可以通过 `async` 和 `await` 关键字实现。 + + ```python + import asyncio + + async def main(): + print("Hello") + await asyncio.sleep(1) + print("World") + + asyncio.run(main()) + ``` + +8. **动态类型(Dynamic Typing)**: + 利用 Python 的动态类型特性,可以在运行时动态地修改对象的属性和方法。 + + ```python + class MyClass: + pass + + obj = MyClass() + obj.new_attribute = "This is a new attribute" + print(obj.new_attribute) + ``` +