Python 的一些高阶用法和特性,以下是一些常见的高阶用法:
-
生成器(Generators): 生成器是一种特殊的迭代器,可以用来生成一系列值。使用
yield
关键字创建生成器函数。def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
for value in gen:
print(value) -
上下文管理器(Context Managers): 使用
with
语句和contextlib
模块来管理资源,如文件、网络连接等。with open('example.txt', 'r') as file:
content = file.read() -
装饰器(Decorators): 装饰器是一种高阶函数,可以在不修改原函数代码的情况下,对其进行扩展或修改。
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() -
类装饰器(Class Decorators): 类装饰器可以用来扩展或修改类的行为。
def class_decorator(cls):
cls.new_attribute = "This is a new attribute"
return cls
@class_decorator
class MyClass:
pass
print(MyClass.new_attribute) -
元类(Metaclasses): 元类是创建类的类。可以用来自定义类的创建行为。
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) -
函数式编程(Functional Programming): 使用
map
、filter
、reduce
等高阶函数进行函数式编程。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) -
协程(Coroutines): 协程是一种用于异步编程的工具,可以通过
async
和await
关键字实现。import asyncio
async def main():
print("Hello")
await asyncio.sleep(1)
print("World")
asyncio.run(main()) -
动态类型(Dynamic Typing): 利用 Python 的动态类型特性,可以在运行时动态地修改对象的属性和方法。
class MyClass:
pass
obj = MyClass()
obj.new_attribute = "This is a new attribute"
print(obj.new_attribute)