-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathexample.py
More file actions
60 lines (47 loc) · 1.26 KB
/
example.py
File metadata and controls
60 lines (47 loc) · 1.26 KB
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
示例Python文件 - 修复版
严格遵循PEP8规范
"""
import os
import sys
from datetime import datetime
class MyClass:
"""示例类,用于存储姓名和年龄信息。"""
def __init__(self):
"""初始化MyClass实例,设置默认姓名为空字符串,年龄为0。"""
self.name: str = ""
self.age: int = 0
def get_info(self) -> str:
"""获取对象的姓名和年龄信息。
Returns:
str: 格式化的姓名和年龄字符串
"""
return f"Name: {self.name}, Age: {self.age}"
def set_name(self, name: str) -> None:
"""设置对象的姓名。
Args:
name (str): 要设置的姓名
"""
self.name = name
def bad_function() -> int:
"""示例函数,演示加法运算和异常处理。
Returns:
int: x + y 的结果
"""
x: int = 1
y: int = 2
z: int = x + y
print(z)
try:
# 注意:此除零操作为示例代码,实际使用时应避免
result: float = 10 / 0
except ZeroDivisionError:
pass
return z
if __name__ == "__main__":
obj = MyClass()
obj.set_name("test")
print(obj.get_info())
bad_function()