{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 编程入门07:Python函数定义\n", "你己经接触过很多的函数,现在让我们尝试定义自己的函数——这需要使用def关键字。函数定义好后即可调用,以下是在交互模式中定义并调用了一个非常简单的函数:\n", "```\n", "In [1]: def welcome():\n", " ...: \"\"\"输出欢迎信息\"\"\"\n", " ...: print(\"欢迎光临!\")\n", " ...: \n", "\n", "In [2]: welcome()\n", "欢迎光临!\n", "\n", "In [3]: type(welcome)\n", "Out[3]: function\n", "\n", "In [4]: help(welcome)\n", "Help on function welcome in module __main__:\n", "\n", "welcome()\n", " 输出欢迎信息\n", "```\n", "你可以发现函数并不神秘,只是把一段程序定义为一个对象以便重复使用而已——另外请注意用三个引号引起来的字符串,这样的字符串允许分行,当放在函数体最前面时则会成为这个函数的“文档字符串”(Docstring),help函数所显示的帮助信息就来自相应对象的文档字符串。你可以使用return语句结束函数并指定返回值,否则默认返回空值。\n", "```\n", "In [5]: print(welcome())\n", "欢迎光临!\n", "None\n", "\n", "In [6]: def iseven(n):\n", " ...: \"\"\"判断是否偶数\"\"\"\n", " ...: return n % 2 == 0\n", " ...: \n", "\n", "In [7]: iseven(99)\n", "Out[7]: False\n", "```\n", "\n", "接下来让我们再用海龟绘图来练习复杂一些的函数:新建文件turtledraw.pyw,定义一个五角星函数star5p,并在之后的代码中调用这个函数:\n", "```\n", "\"\"\"自定义的海龟绘图函数集\n", "\"\"\"\n", "import turtle as tt\n", "\n", "\n", "def star5p():\n", " \"\"\"画一颗五角星\n", " \"\"\"\n", " t = tt.Turtle() # 生成一个单独的海龟对象\n", " t.hideturtle()\n", " t.speed(0)\n", " t.color(\"purple\")\n", " t.penup()\n", " t.begin_fill() # 启用填充区域\n", " cnt = 0\n", " while cnt < 5:\n", " t.forward(20)\n", " t.left(72)\n", " t.forward(20)\n", " t.right(144)\n", " cnt += 1\n", " t.end_fill()\n", "\n", "\n", "tt.TurtleScreen._RUNNING = True # 画一个五边形\n", "tt.color(\"pink\")\n", "tt.penup()\n", "tt.begin_fill()\n", "tt.setpos(-25, -75)\n", "tt.pendown()\n", "cnt = 0\n", "while cnt < 5:\n", " tt.forward(100)\n", " tt.left(72)\n", " cnt += 1\n", "tt.end_fill()\n", "star5p() # 调用函数画一颗五角星\n", "tt.done()\n", "```\n", "函数在所属模块空间内分隔出一个子空间——例如star5p函数生成了一个单独的海龟对象变量t。这个变量t只在star5p函数中存在,离开这个函数就不能再使用了,而star5p函数内则可以使用在外面定义的海龟对象变量tt——变量存在并发挥作用的范围称为变量的“作用域”(Scope),如果要修改变量的作用域,可以用global关键字来声明“全局变量”,用nonlocal关键字来声明“非局部变量”。\n", "\n", "上面程序中定义的star5p函数很不灵活,不管调用多少次都只从原点开始画一颗同样的五角星,让我们来加以改进——定义函数时加上参数变量列表:x和y变量指定五角星的坐标位置;size变量指定五角星的边长,angle变量指定五角星的倾角,在函数调用时作为参数传入的值将赋给这些变量,参数变量作用域限于函数内部:\n", "```\n", "\"\"\"自定义的海龟绘图函数集\n", "\"\"\"\n", "import turtle as tt\n", "\n", "def star5p(x, y, size=20, angle=0):\n", " \"\"\"在指定位置画一颗五角星\n", " \"\"\"\n", " t = tt.Turtle() # 生成一个单独的海龟对象\n", " t.hideturtle()\n", " t.speed(0)\n", " t.color(\"white\")\n", " t.penup()\n", " t.setpos(x, y)\n", " t.right(angle)\n", " t.begin_fill() # 启用填充区域\n", " cnt = 0\n", " while cnt < 5:\n", " t.forward(size)\n", " t.left(72)\n", " t.forward(size)\n", " t.right(144)\n", " cnt += 1\n", " t.end_fill()\n", "\n", "\n", "def test():\n", " \"\"\"测试绘图函数:随机画十颗五角星\n", " \"\"\"\n", " from random import randint\n", " tt.TurtleScreen._RUNNING = True\n", " tt.setup(width=720, height=480, startx=None, starty=None)\n", " tt.hideturtle()\n", " tt.speed(0)\n", " tt.bgcolor(\"purple\")\n", " tt.penup()\n", " cnt = 0\n", " while cnt < 10:\n", " x = randint(-300, 300)\n", " y = randint(-200, 200)\n", " s = randint(10, 30)\n", " a = randint(0, 72)\n", " star5p(x, y, s, a) # 带参数调用五角星函数\n", " cnt += 1\n", " tt.done()\n", "\n", "\n", "if __name__ == \"__main__\": # 运行模块时调用测试绘图函数\n", " test()\n", "```\n", "现在的star5p函数有4个参数,前两个参数在调用时是必须传入的,后两个参数在定义时赋了默认值,在调用时如果不传入就会使用默认值——如果只传入三个参数,则第三个参数必须使用“变量名=值”的形式例如“angle=10”以避免歧义。上面的程序还定义了一个test函数,实现随机绘制十颗五角星:\n", "\n", "\n", "程序文件turtledraw.pyw实际上就是一个自定义模块,可以使用import语句引入。模块在主名称空间中运行时```__name__```变量的取值是默认的```__main__```,被引入时```__name__```变量的取值则是模块名turtledraw,模块就可以据此判断自己是被运行还是被引入,从而决定是否执行test函数——推荐大家今后也都使用这样的模式,整个程序由独立的函数构成,并在最后调用一个作为入口的“主函数”。\n", "\n", "```\n", "In [1]: import turtledraw as td\n", "\n", "In [2]: help(td)\n", "Help on module turtledraw:\n", "\n", "NAME\n", " turtledraw - 自定义的海龟绘图函数集\n", "\n", "FUNCTIONS\n", " star5p(x, y, size=20, angle=0)\n", " 在指定位置画一颗五角星\n", " \n", " test()\n", " 测试绘图函数\n", "\n", "FILE\n", " d:\\test\\pystudy\\turtledraw.pyw\n", "\n", "\n", "\n", "In [3]: td.__name__\n", "Out[3]: 'turtledraw'\n", "\n", "In [4]: \n", "```\n", "\n", "——编程原来是这样……\n", "\n", "## 编程小提示:程序项目\n", "任何IDE都有“工程”或“项目”(Project)的概念,实际上就是建立专门的文件夹来组织相关的程序文件。例如Spyder中创建项目是在主菜单中选择Projects > New Project...,你可以创建一个名为pyStudy的项目,把你编写的所有程序文件都放进去。以后Spyder启动时会自动打开最近的项目并将“工作目录”设为项目所在的文件夹,而不再使用默认的用户文件夹。\n", "\n", "\n", "下一篇:[编程入门08:Python列表类型](08_list.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }