Skip to content

Commit

Permalink
Merge pull request #10 from samzong/samzong-patch-1
Browse files Browse the repository at this point in the history
Create 2024-07-27-python-advanced-usage.md
  • Loading branch information
samzong authored Jul 27, 2024
2 parents 750ac64 + 4b7f649 commit 95392ea
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions blog/2024-07-27-python-advanced-usage.md
Original file line number Diff line number Diff line change
@@ -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)
```

0 comments on commit 95392ea

Please sign in to comment.