add student.py generator

This commit is contained in:
epsilon_luoo
2022-07-13 03:43:28 +08:00
parent 0abf91e434
commit 0357cda026

View File

@@ -69,7 +69,7 @@
"file.write(\"\"\"Hello world!\n",
"Hello Python!!\n",
"Hello smart way!!!\"\"\")\n",
"file.close()\n"
"file.close()"
]
},
{
@@ -307,14 +307,6 @@
"编写模块的方式有很多,其中最简单的模块就是创建一个包含很多函数、变量以及类并以 .py 为后缀的文件。下面我们把上一节中实现的 class 类保存在 student.py 文件中:"
]
},
{
"cell_type": "markdown",
"id": "0894ce6c",
"metadata": {},
"source": [
"**注意!经过新一轮校对后,我们修改了 student 类的代码下图代码已经废弃我们同时在GitHub中提供 student.py 文件供大家参考**"
]
},
{
"cell_type": "markdown",
"id": "46b45d90",
@@ -323,6 +315,49 @@
"<img style=\"float: center;\" src=\"./src/fig44.png\" width=\"50%\"> "
]
},
{
"cell_type": "markdown",
"id": "575e38db",
"metadata": {},
"source": [
"> 为了接下来的模块引入更方便,我们也可以使用下面的代码来直接在当前目录生成 `student.py` 文件。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bc4a2658",
"metadata": {},
"outputs": [],
"source": [
"file = open('student.py', 'w',encoding=\"utf-8\")\n",
"file.write(\"\"\"class student():\n",
" def __init__(self, name, Math_score, Chinese_score):\n",
" self.name = name\n",
" self.Math_score = Math_score\n",
" self.Chinese_score = Chinese_score\n",
" \n",
" ## repr 函数用于定义对象被输出时的输出结果\n",
" def __repr__(self):\n",
" return str((self.name, self.Math_score, self.Chinese_score))\n",
" \n",
" def change_score(self, course_name, score):\n",
" if course_name == 'Math':\n",
" self.Math_score = score\n",
" elif course_name == 'Chinese':\n",
" self.Chinese_score = score\n",
" else:\n",
" print(course_name, \" course is still not in current system\")\n",
" \n",
" def print_name(self,):\n",
" print(self.name)\n",
" \n",
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None\"\"\")\n",
"file.close()"
]
},
{
"cell_type": "markdown",
"id": "ef0e6af7",
@@ -333,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 3,
"id": "8de02c29",
"metadata": {},
"outputs": [],
@@ -343,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 4,
"id": "4c9f7a60",
"metadata": {},
"outputs": [