# Python测开面试题
# Python基础
# 1. Python中的列表和元组有什么区别?
答案: 主要区别在于可变性:
- 列表(List) 是可变的,创建后可以修改其内容(增、删、改元素)。使用方括号
[]定义。 - 元组(Tuple) 是不可变的,一旦创建,其内容无法修改。使用圆括号
()定义。
其他区别:
- 列表有更多内置方法(如append, pop, sort),元组方法较少
- 元组性能稍好,占用内存更少
- 元组可用作字典的键,列表不可以
# 2. 如何创建一个空字典和空集合?
答案:
# 空字典
empty_dict = {}
empty_dict = dict()
# 空集合(不能使用{},因为这是空字典)
empty_set = set()
2
3
4
5
6
# 3. Python中的基本数据类型有哪些?
答案: Python的基本数据类型包括:
- 数字类型:
int(整数),float(浮点数),complex(复数) - 布尔类型:
bool(True/False) - 字符串:
str - 列表:
list - 元组:
tuple - 字典:
dict - 集合:
set
# 4. 如何检查变量的类型?
答案:
使用 type() 函数或 isinstance() 函数:
x = 10
print(type(x)) # <class 'int'>
print(isinstance(x, int)) # True
2
3
# 5. 解释Python中的缩进规则
答案: Python使用缩进来表示代码块,而不是大括号。
- 同一代码块的语句必须有相同的缩进
- 通常使用4个空格作为一级缩进
- 缩进错误会导致
IndentationError
# 6. 如何注释Python代码?
答案:
# 这是单行注释
"""
这是多行注释(实际上是字符串,但常用作注释)
可以写多行内容
"""
'''
这也是多行注释
'''
2
3
4
5
6
7
8
9
10
# 7. 如何从用户获取输入?
答案:
使用 input() 函数:
name = input("请输入你的名字:")
print(f"你好,{name}!")
2
# 8. 如何将字符串转换为数字?
答案:
# 字符串转整数
num_str = "123"
num_int = int(num_str)
# 字符串转浮点数
float_str = "3.14"
num_float = float(float_str)
2
3
4
5
6
7
# 9. 如何将数字转换为字符串?
答案:
num = 123
num_str = str(num)
2
# 10. 如何格式化字符串?
答案: 有三种主要方式:
name = "Alice"
age = 25
# 1. % 格式化
print("姓名:%s,年龄:%d" % (name, age))
# 2. str.format()
print("姓名:{},年龄:{}".format(name, age))
# 3. f-string(推荐,Python 3.6+)
print(f"姓名:{name},年龄:{age}")
2
3
4
5
6
7
8
9
10
11
# 11. 如何创建和调用函数?
答案:
# 定义函数
def greet(name):
return f"Hello, {name}!"
# 调用函数
message = greet("Alice")
print(message) # Hello, Alice!
2
3
4
5
6
7
# 12. 函数中的return语句有什么作用?
答案:
- 结束函数的执行
- 将值返回给调用者
- 如果没有return语句,函数返回None
# 13. 如何定义有默认参数的函数?
答案:
def greet(name, message="Hello"):
return f"{message}, {name}!"
print(greet("Alice")) # Hello, Alice!
print(greet("Bob", "Hi")) # Hi, Bob!
2
3
4
5
# 14. 什么是Python的条件语句?
答案:
使用 if, elif, else 进行条件判断:
age = 18
if age < 13:
print("儿童")
elif age < 18:
print("青少年")
else:
print("成人")
2
3
4
5
6
7
8
# 15. Python中的循环有哪几种?
答案: 两种循环:
for循环:遍历序列中的元素
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)1
2
3while循环:条件为真时重复执行
count = 0 while count < 5: print(count) count += 11
2
3
4
# 16. 如何中断循环?
答案:
使用 break 和 continue:
# break:完全退出循环
for i in range(10):
if i == 5:
break
print(i) # 只打印 0,1,2,3,4
# continue:跳过当前迭代,继续下一次
for i in range(5):
if i == 2:
continue
print(i) # 打印 0,1,3,4
2
3
4
5
6
7
8
9
10
11
# 17. 如何遍历字典?
答案:
person = {"name": "Alice", "age": 25, "city": "New York"}
# 遍历键
for key in person:
print(key)
# 遍历键值对
for key, value in person.items():
print(f"{key}: {value}")
# 只遍历值
for value in person.values():
print(value)
2
3
4
5
6
7
8
9
10
11
12
13
# 18. 如何向列表添加元素?
答案:
fruits = ["apple", "banana"]
# 末尾添加
fruits.append("orange")
# 指定位置插入
fruits.insert(1, "grape")
print(fruits) # ['apple', 'grape', 'banana', 'orange']
2
3
4
5
6
7
8
9
# 19. 如何从列表删除元素?
答案:
numbers = [1, 2, 3, 4, 5, 3]
# 按值删除(第一个匹配项)
numbers.remove(3)
# 按索引删除
del numbers[0]
popped = numbers.pop(1) # 删除并返回该元素
print(numbers) # [2, 4, 5, 3]
2
3
4
5
6
7
8
9
10
# 20. 如何获取列表的长度?
答案:
使用 len() 函数:
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # 3
2
# 21. 如何对列表进行排序?
答案:
numbers = [3, 1, 4, 1, 5, 9, 2]
# 升序排序(改变原列表)
numbers.sort()
print(numbers) # [1, 1, 2, 3, 4, 5, 9]
# 降序排序
numbers.sort(reverse=True)
# 创建新排序列表(不改变原列表)
sorted_numbers = sorted(numbers)
2
3
4
5
6
7
8
9
10
11
# 22. 如何复制列表?
答案:
original = [1, 2, 3]
# 浅拷贝
shallow_copy = original.copy()
shallow_copy2 = list(original)
shallow_copy3 = original[:] # 切片复制
# 注意:对于嵌套列表,需要深拷贝
import copy
deep_copy = copy.deepcopy(original)
2
3
4
5
6
7
8
9
10
# 23. 如何检查元素是否在列表中?
答案:
使用 in 关键字:
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("orange" in fruits) # False
2
3
# 24. 字符串的常用方法有哪些?
答案:
text = " Hello World! "
print(text.strip()) # "Hello World!"(去空格)
print(text.lower()) # " hello world! "(转小写)
print(text.upper()) # " HELLO WORLD! "(转大写)
print(text.replace("Hello", "Hi")) # " Hi World! "
print(text.split()) # ['Hello', 'World!'](分割)
print("hello".capitalize()) # "Hello"(首字母大写)
2
3
4
5
6
7
8
# 25. 如何连接两个列表?
答案:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# 方法1:+ 运算符
combined = list1 + list2
# 方法2:extend() 方法
list1.extend(list2)
# 方法3:切片赋值
list1[len(list1):] = list2
2
3
4
5
6
7
8
9
10
11
# 26. 什么是字典?如何创建?
答案: 字典是键值对的集合:
# 创建字典
person = {"name": "Alice", "age": 25, "city": "New York"}
# 访问值
print(person["name"]) # Alice
print(person.get("age")) # 25
# 添加/修改键值对
person["email"] = "alice@example.com"
person["age"] = 26
2
3
4
5
6
7
8
9
10
# 27. 如何删除字典中的键值对?
答案:
person = {"name": "Alice", "age": 25}
# 删除指定键
del person["age"]
# 删除并返回值
age = person.pop("age", None) # 第二个参数是默认值
# 清空字典
person.clear()
2
3
4
5
6
7
8
9
10
# 28. 如何获取字典的所有键和值?
答案:
person = {"name": "Alice", "age": 25}
keys = person.keys() # 所有键
values = person.values() # 所有值
items = person.items() # 所有键值对
print(list(keys)) # ['name', 'age']
print(list(values)) # ['Alice', 25]
2
3
4
5
6
7
8
# 29. 什么是集合?有什么特点?
答案: 集合是无序且元素唯一的数据结构:
# 创建集合
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits) # {'apple', 'banana', 'cherry'}(自动去重)
# 添加元素
fruits.add("orange")
# 删除元素
fruits.remove("banana")
# 集合运算
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2) # 并集: {1, 2, 3, 4, 5}
print(set1 & set2) # 交集: {3}
print(set1 - set2) # 差集: {1, 2}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 30. 如何读取文件内容?
答案:
# 读取整个文件
with open("file.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
# 逐行读取
with open("file.txt", "r") as file:
for line in file:
print(line.strip()) # 去掉换行符
2
3
4
5
6
7
8
9
# 31. 如何写入文件?
答案:
# 写入文件(覆盖)
with open("file.txt", "w") as file:
file.write("Hello World!\n")
file.write("第二行内容")
# 追加内容
with open("file.txt", "a") as file:
file.write("\n追加的内容")
2
3
4
5
6
7
8
# 32. 什么是异常处理?
答案:
使用 try-except 处理程序中的错误:
try:
num = int(input("请输入数字:"))
result = 10 / num
print(f"结果是:{result}")
except ValueError:
print("输入的不是有效数字!")
except ZeroDivisionError:
print("不能除以零!")
except Exception as e:
print(f"发生错误:{e}")
else:
print("没有发生错误")
finally:
print("无论是否出错都会执行")
2
3
4
5
6
7
8
9
10
11
12
13
14
# 33. 如何抛出异常?
答案:
使用 raise 语句:
def check_age(age):
if age < 0:
raise ValueError("年龄不能为负数")
if age < 18:
raise ValueError("未成年不允许访问")
return True
try:
check_age(-5)
except ValueError as e:
print(e) # 年龄不能为负数
2
3
4
5
6
7
8
9
10
11
# 34. 如何定义和使用模块?
答案:
创建 mymodule.py:
# mymodule.py
def greet(name):
return f"Hello, {name}!"
PI = 3.14159
2
3
4
5
使用模块:
import mymodule
print(mymodule.greet("Alice"))
print(mymodule.PI)
# 或者
from mymodule import greet, PI
print(greet("Bob"))
2
3
4
5
6
7
8
# 35. 什么是Python包?
答案:
包是包含多个模块的文件夹,必须有 __init__.py 文件:
mypackage/
__init__.py
module1.py
module2.py
subpackage/
__init__.py
module3.py
2
3
4
5
6
7
使用包:
from mypackage import module1
from mypackage.subpackage import module3
2
# 36. 如何获取当前日期时间?
答案:
from datetime import datetime, date
# 当前日期时间
now = datetime.now()
print(now) # 2023-10-15 14:30:25.123456
# 当前日期
today = date.today()
print(today) # 2023-10-15
# 格式化日期
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted) # 2023-10-15 14:30:25
2
3
4
5
6
7
8
9
10
11
12
13
# 37. 如何计算两个日期的差值?
答案:
from datetime import datetime, timedelta
date1 = datetime(2023, 10, 1)
date2 = datetime(2023, 10, 15)
difference = date2 - date1
print(difference.days) # 14
# 日期加减
new_date = date1 + timedelta(days=7)
print(new_date) # 2023-10-08
2
3
4
5
6
7
8
9
10
11
# 38. 如何使用随机数?
答案:
import random
# 随机整数
print(random.randint(1, 10)) # 1-10之间的随机整数
# 随机浮点数
print(random.random()) # 0-1之间的随机浮点数
# 随机选择
fruits = ["apple", "banana", "cherry"]
print(random.choice(fruits)) # 随机选择一个元素
# 随机打乱
random.shuffle(fruits)
print(fruits) # 打乱后的列表
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 39. 如何安装第三方包?
答案: 使用 pip 命令:
# 安装包
pip install package_name
# 安装特定版本
pip install package_name==1.2.3
# 从requirements文件安装
pip install -r requirements.txt
# 升级包
pip install --upgrade package_name
# 卸载包
pip uninstall package_name
2
3
4
5
6
7
8
9
10
11
12
13
14
# 40. 常用的内置函数有哪些?
答案:
# 类型转换
int("123"), float("3.14"), str(123), list((1,2,3)), tuple([1,2,3])
# 数学运算
abs(-5), round(3.14159, 2), min([1,2,3]), max([1,2,3]), sum([1,2,3])
# 迭代相关
len("hello"), range(5), enumerate(["a", "b"]), zip([1,2], ["a","b"])
# 输入输出
print("hello"), input("请输入:"), open("file.txt")
# 其他
isinstance(5, int), type("hello"), id(obj), help(print)
2
3
4
5
6
7
8
9
10
11
12
13
14
# 41. 如何定义类?
答案:
class Person:
# 类属性
species = "Human"
# 初始化方法
def __init__(self, name, age):
# 实例属性
self.name = name
self.age = age
# 实例方法
def introduce(self):
return f"我叫{self.name},今年{self.age}岁"
# 类方法
@classmethod
def from_birth_year(cls, name, birth_year):
age = datetime.now().year - birth_year
return cls(name, age)
# 创建实例
person1 = Person("Alice", 25)
person2 = Person.from_birth_year("Bob", 2000)
print(person1.introduce())
print(Person.species)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 42. 什么是继承?
答案:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal): # 继承Animal类
def speak(self):
return "汪汪!"
class Cat(Animal):
def speak(self):
return "喵喵!"
dog = Dog("旺财")
cat = Cat("咪咪")
print(dog.speak()) # 汪汪!
print(cat.speak()) # 喵喵!
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 43. 如何重写方法?
答案:
class Parent:
def method(self):
print("父类方法")
class Child(Parent):
def method(self): # 重写父类方法
print("子类方法")
super().method() # 调用父类方法
child = Child()
child.method()
2
3
4
5
6
7
8
9
10
11
# 44. 什么是多态?
答案: 不同类的对象对同一消息做出不同的响应:
class Circle:
def draw(self):
print("绘制圆形")
class Square:
def draw(self):
print("绘制方形")
def draw_shape(shape):
shape.draw() # 多态调用
circle = Circle()
square = Square()
draw_shape(circle) # 绘制圆形
draw_shape(square) # 绘制方形
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 45. 如何使用super()函数?
答案: super() 用于调用父类的方法:
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):
def __init__(self, name, age):
super().__init__(name) # 调用父类的__init__
self.age = age
child = Child("Alice", 25)
print(child.name, child.age) # Alice 25
2
3
4
5
6
7
8
9
10
11
# 46. 什么是@property装饰器?
答案: 将方法转换为属性:
class Person:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
@property
def full_name(self):
return f"{self.first_name} {self.last_name}"
@full_name.setter
def full_name(self, name):
first, last = name.split()
self.first_name = first
self.last_name = last
person = Person("Alice", "Smith")
print(person.full_name) # Alice Smith
person.full_name = "Bob Johnson"
print(person.first_name) # Bob
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 47. 如何遍历数字序列?
答案:
使用 range() 函数:
# 生成0-4的数字
for i in range(5):
print(i)
# 生成5-9的数字
for i in range(5, 10):
print(i)
# 生成0-10的偶数
for i in range(0, 11, 2):
print(i)
2
3
4
5
6
7
8
9
10
11
# 48. 如何反转列表?
答案:
numbers = [1, 2, 3, 4, 5]
# 方法1:reverse()方法(修改原列表)
numbers.reverse()
print(numbers) # [5, 4, 3, 2, 1]
# 方法2:切片(创建新列表)
reversed_numbers = numbers[::-1]
print(reversed_numbers) # [5, 4, 3, 2, 1]
# 方法3:reversed()函数
reversed_iter = reversed(numbers)
print(list(reversed_iter)) # [5, 4, 3, 2, 1]
2
3
4
5
6
7
8
9
10
11
12
13
# 49. 如何检查文件是否存在?
答案:
import os
file_path = "example.txt"
# 检查文件是否存在
if os.path.exists(file_path):
print("文件存在")
# 检查是否是文件
if os.path.isfile(file_path):
print("这是一个文件")
# 检查是否是目录
if os.path.isdir(file_path):
print("这是一个目录")
else:
print("文件不存在")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Python进阶
# 1. Python中的列表和元组有什么区别?
答案: 主要区别在于可变性:
- 列表(List) 是可变的,创建后可以修改其内容(增、删、改元素)。使用方括号
[]定义。性能稍低,占用更多内存。 - 元组(Tuple) 是不可变的,一旦创建,其内容无法修改。使用圆括号
()定义。性能更高,可用作字典的键。
# 2. 解释Python的字典和集合是如何实现的
答案: 两者都基于哈希表实现。
- 字典:存储键值对。键必须是不可变对象(如字符串、数字、元组),Python对键求哈希值来确定存储位置,实现O(1)时间复杂度的查找。
- 集合:存储唯一元素。同样使用哈希表,只有键没有值,用于快速成员检测和去重。
# 3. 什么是生成器?与列表有何不同?
答案: 生成器是一种特殊的迭代器,使用 yield 关键字定义。它惰性计算值,即用到时才生成,节省内存。
与列表的区别:
- 列表一次性生成所有数据,占用内存大
- 生成器按需生成数据,占用内存小
- 生成器只能迭代一次,列表可多次使用
# 列表推导式
list_comp = [x*x for x in range(1000000)] # 占用大量内存
# 生成器表达式
gen_exp = (x*x for x in range(1000000)) # 几乎不占内存
2
3
4
5
# 4. 解释*args和**kwargs的作用
答案:
*args:接收任意数量的位置参数,在函数内作为元组处理**kwargs:接收任意数量的关键字参数,在函数内作为字典处理
def example_func(a, b, *args, **kwargs):
print(f"a: {a}, b: {b}")
print(f"args: {args}") # 元组
print(f"kwargs: {kwargs}") # 字典
example_func(1, 2, 3, 4, 5, name='Alice', age=25)
2
3
4
5
6
# 5. Python中的深拷贝和浅拷贝有什么区别?
答案:
- 浅拷贝:创建新对象,但子对象是原对象的引用。使用
copy.copy() - 深拷贝:完全递归拷贝,新对象与原对象完全独立。使用
copy.deepcopy()
import copy
original = [1, 2, [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)
original[2][0] = 'changed'
print(shallow) # [1, 2, ['changed', 4]] 受影响
print(deep) # [1, 2, [3, 4]] 不受影响
2
3
4
5
6
7
8
9
# 6. 什么是装饰器?写一个简单的装饰器示例
答案: 装饰器是修改或增强函数行为的函数,接收函数作为参数并返回新函数。
def timer_decorator(func):
def wrapper(*args, **kwargs):
import time
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} executed in {end-start:.4f}s")
return result
return wrapper
@timer_decorator
def slow_function():
time.sleep(2)
slow_function() # 输出: slow_function executed in 2.0002s
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 7. 解释Python的GIL及其影响
答案: GIL(全局解释器锁) 是CPython解释器中的互斥锁,防止多个线程同时执行Python字节码。 影响:
- 无法实现真正的多线程并行(CPU密集型任务)
- I/O密集型任务影响较小(等待I/O时释放GIL)
- 多进程可绕过GIL限制
# 8. 什么是上下文管理器?with语句如何工作?
答案:
上下文管理器管理资源的获取和释放,通过实现 __enter__() 和 __exit__() 方法。
class FileManager:
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file
def __exit__(self, exc_type, exc_val, exc_tb):
self.file.close()
with FileManager('test.txt', 'w') as f:
f.write('hello')
2
3
4
5
6
7
8
9
10
11
12
13
14
# 9. Python的内存管理机制是怎样的?
答案:
- 引用计数:对象被引用时计数+1,引用解除时-1,计数为0时回收
- 垃圾回收:解决循环引用问题,分代回收策略
- 内存池:管理小内存分配,提高效率
# 10. 解释描述符(Descriptor)的概念
答案:
描述符是实现了 __get__, __set__, __delete__ 方法的对象,用于管理属性访问。
class PositiveNumber:
def __init__(self, name):
self.name = name
def __get__(self, instance, owner):
return instance.__dict__[self.name]
def __set__(self, instance, value):
if value <= 0:
raise ValueError("Positive number required")
instance.__dict__[self.name] = value
class Person:
age = PositiveNumber('age')
p = Person()
p.age = 25 # 正确
p.age = -5 # 抛出 ValueError
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 11. 什么是元类?它有什么用途?
答案:
元类是类的类,用于创建类。可通过继承 type 类来自定义元类。
class Meta(type):
def __new__(cls, name, bases, dct):
# 在类创建前修改属性
dct['modified'] = True
return super().__new__(cls, name, bases, dct)
class MyClass(metaclass=Meta):
pass
print(MyClass.modified) # True
2
3
4
5
6
7
8
9
10
# 12. 解释Python的多继承和MRO
答案: MRO(方法解析顺序) 决定多继承时方法的查找顺序,使用C3线性化算法。
class A:
def method(self):
print("A")
class B(A):
def method(self):
print("B")
class C(A):
def method(self):
print("C")
class D(B, C):
pass
d = D()
d.method() # 输出: B
print(D.__mro__) # 显示方法解析顺序
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 13. 什么是协程?与线程有何区别?
答案: 协程是用户态的轻量级线程,由程序员控制调度。 区别:
- 协程切换开销小,线程切换开销大
- 协程在单线程内运行,无需锁机制
- 协程利用异步I/O提高并发性
# 14. 解释Python的垃圾回收机制
答案:
- 引用计数:主要机制,实时回收
- 标记-清除:解决循环引用问题
- 分代回收:根据对象存活时间分为三代,不同频率回收
# 15. 什么是鸭子类型?
答案: "如果它走起来像鸭子,叫起来像鸭子,那么它就是鸭子。"关注对象的行为而非类型。
class Duck:
def quack(self):
print("Quack!")
class Person:
def quack(self):
print("I'm quacking like a duck!")
def make_it_quack(thing):
thing.quack() # 不检查类型,只关心是否有quack方法
make_it_quack(Duck()) # Quack!
make_it_quack(Person()) # I'm quacking like a duck!
2
3
4
5
6
7
8
9
10
11
12
13
# 16. Python中的单下划线和双下划线有什么含义?
答案:
_var:内部使用约定(import * 时不导入)__var:名称修饰(变成_Classname__var)__var__:特殊方法var_:避免与关键字冲突
# 17. 解释Python的闭包概念
答案: 闭包是能够访问其他函数作用域变量的函数。
def outer_func(x):
def inner_func(y):
return x + y # 访问外部函数的变量x
return inner_func
closure = outer_func(10)
print(closure(5)) # 15
2
3
4
5
6
7
# 18. 什么是Python的WSGI?
答案: WSGI(Web服务器网关接口) 是Web服务器与Python Web应用之间的标准接口。
def simple_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [b'Hello World!']
2
3
4
5
# 19. 解释Python的异步编程asyncio
答案: asyncio 是Python的异步I/O框架,使用 async/await 语法。
import asyncio
async def fetch_data():
print("开始获取数据")
await asyncio.sleep(2)
print("数据获取完成")
return {"data": 123}
async def main():
task = asyncio.create_task(fetch_data())
result = await task
print(f"结果: {result}")
asyncio.run(main())
2
3
4
5
6
7
8
9
10
11
12
13
14
# 20. Python中的属性管理有哪些方式?
答案:
- @property:将方法转为属性
- 描述符:精细控制属性访问
- getattr 和 setattr:自定义属性访问
class Person:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
@property
def full_name(self):
return f"{self.first_name} {self.last_name}"
@full_name.setter
def full_name(self, name):
first, last = name.split(' ')
self.first_name = first
self.last_name = last
p = Person("John", "Doe")
print(p.full_name) # John Doe
p.full_name = "Jane Smith"
print(p.first_name) # Jane
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 21. 解释Python的模块和包
答案:
- 模块:单个Python文件(.py)
- 包:包含
__init__.py的目录,可包含多个模块 - 导入路径:
sys.path决定模块查找路径
# 22. 什么是Python的字节码?
答案: Python源代码编译后的中间形式,保存在.pyc文件中,由Python虚拟机执行。
# 23. 解释Python的装饰器堆叠
答案: 多个装饰器可以堆叠使用,从下往上执行。
def decorator1(func):
def wrapper():
print("Decorator 1")
func()
return wrapper
def decorator2(func):
def wrapper():
print("Decorator 2")
func()
return wrapper
@decorator1
@decorator2
def my_function():
print("Original function")
my_function()
# 输出:
# Decorator 1
# Decorator 2
# Original function
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 24. Python中的元组拆包是什么?
答案: 将元组元素分配给多个变量。
# 基本拆包
a, b, c = (1, 2, 3)
# 星号表达式
first, *middle, last = [1, 2, 3, 4, 5]
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5
# 交换变量
a, b = b, a
2
3
4
5
6
7
8
9
10
11
# 25. 解释Python的枚举类型
答案: Enum 类用于创建枚举类型。
from enum import Enum, auto
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# 或者使用 auto()
WHITE = auto()
BLACK = auto()
print(Color.RED) # Color.RED
print(Color.RED.value) # 1
print(Color.WHITE.value) # 4
2
3
4
5
6
7
8
9
10
11
12
13
# 26. 什么是Python的数据类?
答案: @dataclass 装饰器自动生成特殊方法,简化类的创建。
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
z: int = 0 # 默认值
p = Point(1, 2)
print(p) # Point(x=1, y=2, z=0)
2
3
4
5
6
7
8
9
10
# 27. 解释Python的类型提示
答案: Python 3.5+ 支持类型提示,提高代码可读性和可维护性。
from typing import List, Dict, Optional, Union
def process_items(
items: List[str],
counts: Dict[str, int],
optional_param: Optional[str] = None
) -> Union[str, int]:
# 函数体
return "result"
2
3
4
5
6
7
8
9
# 28. Python中的协程是如何实现的?
答案:
基于生成器实现,使用 yield from 和 async/await。
# 旧式协程(基于生成器)
def old_coroutine():
while True:
received = yield
print(f"Received: {received}")
# 新式协程(async/await)
async def new_coroutine():
await asyncio.sleep(1)
return "done"
2
3
4
5
6
7
8
9
10
# 29. 解释Python的魔术方法
答案: 以双下划线开头和结尾的特殊方法。
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 3)
v2 = Vector(1, 1)
print(v1 + v2) # Vector(3, 4)
2
3
4
5
6
7
8
9
10
11
12
13
14
# 30. Python中的并发编程有哪些方式?
答案:
- 多线程:
threading模块,适合I/O密集型 - 多进程:
multiprocessing模块,适合CPU密集型 - 协程:
asyncio模块,适合高并发I/O - 并发 futures:
concurrent.futures高级接口
# 31. 解释Python的包管理工具
答案:
- pip:Python包安装工具
- virtualenv:创建隔离的Python环境
- poetry:现代依赖管理和打包工具
- conda:跨平台包和环境管理器
# 32. 什么是Python的__slots__?
答案: __slots__ 限制类实例的属性,节省内存。
class Person:
__slots__ = ['name', 'age'] # 只能有这些属性
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("John", 30)
p.name = "Jane" # 正确
p.address = "123 St" # AttributeError
2
3
4
5
6
7
8
9
10
# 33. 解释Python的上下文管理器协议
答案:
实现 __enter__() 和 __exit__() 方法。
class CustomContext:
def __enter__(self):
print("Entering context")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting context")
if exc_type:
print(f"Exception handled: {exc_val}")
return True # 抑制异常
with CustomContext() as ctx:
print("Inside context")
raise ValueError("test error") # 被抑制
2
3
4
5
6
7
8
9
10
11
12
13
14
# 34. Python中的函数参数传递是值传递还是引用传递?
答案: Python采用共享传参(Call by sharing):
- 不可变对象(数字、字符串、元组)表现为值传递
- 可变对象(列表、字典)表现为引用传递
# 35. 解释Python的生成器表达式
答案: 类似列表推导式,但返回生成器对象。
# 列表推导式 - 立即计算
squares_list = [x*x for x in range(10)]
# 生成器表达式 - 惰性计算
squares_gen = (x*x for x in range(10))
print(squares_list) # [0, 1, 4, ..., 81]
print(squares_gen) # <generator object>
2
3
4
5
6
7
8
# 36. 什么是Python的functools模块?
答案: functools 提供高阶函数和函数操作工具。
from functools import lru_cache, partial
# LRU缓存
@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
# 偏函数
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
print(square(5)) # 25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 37. 解释Python的迭代器协议
答案:
实现 __iter__() 和 __next__() 方法。
class CountDown:
def __init__(self, start):
self.current = start
self.start = start
def __iter__(self):
return self
def __next__(self):
if self.current < 0:
raise StopIteration
else:
self.current -= 1
return self.current + 1
for num in CountDown(3):
print(num) # 3, 2, 1, 0
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 38. Python中的类方法和静态方法有什么区别?
答案:
- 实例方法:接收
self参数,操作实例属性 - 类方法:接收
cls参数,用@classmethod装饰,操作类属性 - 静态方法:不接收特殊参数,用
@staticmethod装饰,与类相关但不需要访问类或实例状态
# 39. 解释Python的元编程概念
答案: 元编程是编写操作代码的代码,包括:
- 装饰器
- 元类
- 动态属性访问
- 代码生成
# 40. 什么是Python的猴子补丁?
答案: 运行时动态修改类或模块。
class Original:
def method(self):
return "original"
def new_method(self):
return "patched"
# 打猴子补丁
Original.method = new_method
obj = Original()
print(obj.method()) # patched
2
3
4
5
6
7
8
9
10
11
12
# 41. Python中的调试技巧有哪些?
答案:
- pdb:Python调试器
- print调试:最简单的调试方法
- logging:日志记录
- 断点:
breakpoint()函数(Python 3.7+) - IDE调试工具:PyCharm、VSCode等
# 42. 解释Python的字符串驻留
答案: Python会缓存一些字符串,多个相同字符串共享同一内存位置。
a = "hello"
b = "hello"
print(a is b) # True - 小字符串被驻留
c = "hello world!"
d = "hello world!"
print(c is d) # False - 长字符串可能不被驻留
2
3
4
5
6
7
# 43. Python中的性能优化技巧有哪些?
答案:
- 使用局部变量
- 避免全局查找
- 使用生成器代替列表
- 使用
join()连接字符串 - 使用适当的数据结构
- 利用内置函数
- 避免不必要的拷贝
# 44. 解释Python的包发布流程
答案:
- 创建
setup.py或pyproject.toml - 配置包元数据
- 构建包:
python -m build - 上传到PyPI:
twine upload dist/*
# 45. 什么是Python的wheel格式?
答案: Wheel是Python的二进制包格式,安装速度比源码包快,不需要编译。
# 46. 解释Python的虚拟环境重要性
答案: 虚拟环境提供隔离的Python运行环境:
- 避免包版本冲突
- 项目依赖隔离
- 便于依赖管理
# 47. Python中的函数式编程特性有哪些?
答案:
- 一等函数(函数作为参数和返回值)
- 高阶函数(map、filter、reduce)
- 匿名函数(lambda)
- 闭包
- 装饰器
# 48. 解释Python的并发陷阱
答案:
- GIL限制:CPU密集型任务无法并行
- 线程安全:共享数据需要同步
- 死锁: improper lock usage
- 资源竞争:未正确同步的并发访问
# 49. Python中的模式匹配是什么?
答案: Python 3.10+ 引入的模式匹配功能。
def match_example(value):
match value:
case [x, y, *rest]:
print(f"List with first two: {x}, {y}")
case {"name": name, "age": age}:
print(f"Person: {name}, {age}")
case int() | float():
print("Number")
case _:
print("Something else")
2
3
4
5
6
7
8
9
10
# 50. 解释Python的异步上下文管理器
答案:
实现 __aenter__() 和 __aexit__() 方法。
class AsyncContextManager:
async def __aenter__(self):
print("Async enter")
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
print("Async exit")
async def main():
async with AsyncContextManager() as cm:
print("Inside async context")
asyncio.run(main())
2
3
4
5
6
7
8
9
10
11
12
13
# 51. Python中的描述符协议有哪些方法?
答案:
__get__(self, instance, owner):获取属性值__set__(self, instance, value):设置属性值__delete__(self, instance):删除属性
# 52. 解释Python的枚举高级用法
答案:
from enum import Enum, auto, unique
@unique # 确保值唯一
class Status(Enum):
PENDING = auto()
RUNNING = auto()
COMPLETED = auto()
FAILED = auto()
def is_finished(self):
return self in [Status.COMPLETED, Status.FAILED]
print(Status.PENDING.is_finished()) # False
2
3
4
5
6
7
8
9
10
11
12
13
# 53. Python中的属性访问控制有哪些方式?
答案:
- 公有属性:正常访问
- 保护属性:单下划线约定
_var - 私有属性:双下划线名称修饰
__var - 属性装饰器:
@property控制访问
# 54. 解释Python的协程状态
答案: 协程可以有多种状态:
GEN_CREATED:已创建未启动GEN_RUNNING:正在执行GEN_SUSPENDED:在yield处暂停GEN_CLOSED:执行结束
# 55. Python中的内存视图是什么?
答案: memoryview 允许在不复制的情况下访问对象的内部数据。
data = bytearray(b'hello')
mv = memoryview(data)
print(mv[0]) # 104 (ASCII 'h')
# 修改视图会影响原数据
mv[0] = 106 # 'j'
print(data) # bytearray(b'jello')
2
3
4
5
6
7
# 56. 解释Python的结构模式匹配
答案: Python 3.10+ 的模式匹配可以匹配复杂数据结构。
def process_data(data):
match data:
case {"type": "point", "x": x, "y": y}:
return f"Point at ({x}, {y})"
case {"type": "circle", "center": center, "radius": r}:
return f"Circle center {center}, radius {r}"
case _:
return "Unknown shape"
2
3
4
5
6
7
8
# 57. Python中的数据序列化格式有哪些?
答案:
- pickle:Python专用二进制格式
- json:跨语言文本格式
- msgpack:高效的二进制格式
- yaml:人类可读的数据格式
- protobuf:高效的二进制格式,需要 schema
# 58. 解释Python的并发编程模型
答案:
- 多线程:共享内存,GIL限制
- 多进程:内存不共享,进程间通信
- 异步编程:单线程事件循环
- Actor模型:通过消息传递通信
# 59. Python中的类型系统特性有哪些?
答案:
- 动态类型:运行时确定类型
- 强类型:不允许隐式类型转换
- 鸭子类型:关注行为而非类型
- 类型提示:可选静态类型检查
# 60. 解释Python的包相对导入
答案: 在包内使用相对路径导入。
# 在 package/module.py 中
from . import sibling_module # 导入同级模块
from .sibling_module import func # 导入同级模块的函数
from .. import parent_module # 导入父级包模块
2
3
4
# 61. Python中的上下文变量是什么?
答案: contextvars 模块提供上下文局部变量,在异步代码中保持状态。
import contextvars
request_id = contextvars.ContextVar('request_id')
def process_request():
print(f"Processing request {request_id.get()}")
async def handle_request(id):
request_id.set(id)
await process_request()
2
3
4
5
6
7
8
9
10
# 62. 解释Python的元类编程应用
答案: 元类可以用于:
- 自动注册子类
- 验证类属性
- 自动生成方法
- ORM框架实现
# 63. Python中的函数缓存有哪些方式?
答案:
functools.lru_cache:LRU缓存装饰器- 自定义缓存字典
- 外部缓存系统(Redis等)
# 64. 解释Python的异步迭代器
答案:
实现 __aiter__() 和 __anext__() 方法。
class AsyncCounter:
def __init__(self, stop):
self.current = 0
self.stop = stop
def __aiter__(self):
return self
async def __anext__(self):
if self.current >= self.stop:
raise StopAsyncIteration
await asyncio.sleep(0.1)
self.current += 1
return self.current - 1
async def main():
async for number in AsyncCounter(3):
print(number)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 65. Python中的协议是什么?
答案:
协议是鸭子类型的正式化,通过 typing.Protocol 定义接口。
from typing import Protocol
class Flyer(Protocol):
def fly(self) -> str: ...
class Bird:
def fly(self):
return "Flying"
class Airplane:
def fly(self):
return "Flying high"
def make_it_fly(flyer: Flyer):
print(flyer.fly())
make_it_fly(Bird()) # Flying
make_it_fly(Airplane()) # Flying high
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 66. 解释Python的包资源管理
答案:
使用 importlib.resources 访问包内资源文件。
from importlib import resources
with resources.open_text('mypackage', 'data.txt') as f:
data = f.read()
with resources.path('mypackage.images', 'logo.png') as path:
print(f"Logo path: {path}")
2
3
4
5
6
7
# 67. Python中的描述符使用场景有哪些?
答案:
- 属性验证
- 惰性求值
- 观察者模式
- ORM字段映射
# 68. 解释Python的异步生成器
答案:
使用 async for 和 yield 的生成器。
async def async_generator():
for i in range(3):
await asyncio.sleep(0.1)
yield i
async def main():
async for value in async_generator():
print(value)
2
3
4
5
6
7
8
# 69. Python中的单例模式实现方式有哪些?
答案:
- 模块导入
- 装饰器
- 元类
__new__方法