05. LLaMA3 Block Tutorial代码笔记
2026/6/25 14:00:30
标识符是用来给变量、函数、类、模块等命名的字符序列,必须符合以下硬性要求:
name、age18、user_name、_score❌ 非法示例:18age(以数字开头)、user-name(含减号)、my@email(含特殊符号)、姓名(非 ASCII 字符,虽部分 Python 环境能运行,但不推荐)Name和name是两个完全不同的标识符,AGE和age也互不相关。if、else、for、def等)不能作为标识符。可通过以下代码查看所有 Python 关键字:python
运行
import keyword print(keyword.kwlist)❌ 错误示例:def = 10(def是关键字)、class = "student"(class是关键字)PEP 8 是 Python 官方的代码风格指南,以下是标识符命名的核心规范,也是行业通用标准:
python
运行
# 变量 user_name = "张三" student_age = 20 total_score = 95.5 # 函数 def calculate_average_score(scores): return sum(scores) / len(scores)python
运行
MAX_RETRY = 3 # 最大重试次数 PI = 3.1415926 # 圆周率 DEFAULT_TIMEOUT = 10 # 默认超时时间python
运行
class StudentInfo: # 学生信息类 def __init__(self, name, age): self.name = name self.age = age class OrderProcessing: # 订单处理类 pass_xxx):约定俗成的 “私有” 标识,提示外部不要直接访问(仅靠约定,语法上仍可访问)。✅ 示例:python
运行
def _private_function(): # 私有函数,仅内部使用 return "内部数据" class Person: def __init__(self): self._id = 123 # 私有属性__xxx):Python 会自动做 “名称修饰”,真正限制外部访问(避免子类重写或外部调用)。✅ 示例:python
运行
class Person: def __init__(self): self.__password = "123456" # 强私有属性 p = Person() print(p.__password) # 直接访问会报错 print(p._Person__password) # 特殊方式可访问(不推荐)a、b、x1),名称要体现用途。❌ 不好的示例:s = "张三"、n = 20✅ 好的示例:student_name = "张三"、student_age = 20calculate_the_average_score_of_all_students),也不要过短(如avg),兼顾简洁和清晰(推荐calculate_student_avg_score)。l(小写 L)、O(大写 O)、0(数字 0),容易看错。snake_case)、常量全大写、类名用大驼峰(CamelCase);