06 2024 档案
摘要:1、文件读写 # 写入文件 with open('example.txt', 'w') as file: file.write('Hello, World!') # 读取文件 with open('example.txt', 'r') as file: content = file.read() p
阅读全文
摘要:单例模式 # 方法 1: 使用类变量 class Singleton: _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__
阅读全文
摘要:组合 class Engine: """引擎类,提供基本的引擎功能""" def __init__(self, power): self.power = power def start(self): print(f"引擎启动,功率:{self.power}") class Car: """汽车类,使
阅读全文
摘要:函数的特殊参数 # /前的参数只能是位置参数,*后面的只能是关键字参数,之间的不限参数类型 def func(a, b, /, c, *, d, e): print(a, b, c, d, e) func(1, 2, 3, d=4, e=5) func(1, 2, c=3, d=4, e=5) #
阅读全文
摘要:格式化输出 %[(name)][flags][width].[precision] typecode (name) :可选,用于选择指定的 key flags:可选,可供选择的值有: – + 右对齐:正数的加正号,负数的加负号 – - 左对齐:正数前没有负号,负数前加负号 width:可选,占有宽度
阅读全文