[{"data":1,"prerenderedAt":1264},["ShallowReactive",2],{"post-2025-07-07-python":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":14,"_type":1258,"_id":1259,"_source":1260,"_file":1261,"_stem":1262,"_extension":1263},"\u002Fblog\u002F2025-07-07-python","blog",false,"","Python-函数","Python函数详解：定义、参数类型、返回值、lambda表达式与装饰器入门。","2025-07-07",[12,13],"Python","基础",{"type":15,"children":16,"toc":1251},"root",[17,22,29,35,56,112,193,201,222,304,309,314,328,359,364,369,386,395,400,418,428,448,453,506,511,574,579,584,598,606,611,616,666,694,725,747,752,757,782,802,825,843,905,910,924,929,937,942,947,970,975,983,999,1004,1024,1102,1107,1112,1117,1122,1135,1149,1157,1162,1175,1189,1232,1245],{"type":18,"tag":19,"props":20,"children":21},"element","hr",{},[],{"type":18,"tag":23,"props":24,"children":26},"h3",{"id":25},"函数的创建和调用",[27],{"type":28,"value":25},"text",{"type":18,"tag":30,"props":31,"children":33},"h4",{"id":32},"创建一个函数",[34],{"type":28,"value":32},{"type":18,"tag":36,"props":37,"children":38},"p",{},[39,45,47,54],{"type":18,"tag":40,"props":41,"children":42},"strong",{},[43],{"type":28,"value":44},"创建函数",{"type":28,"value":46},"也称定义函数，可以理解为创建一个具有某种用途的工具，Python中使用 ",{"type":18,"tag":48,"props":49,"children":51},"code",{"className":50},[],[52],{"type":28,"value":53},"def",{"type":28,"value":55}," 关键字来定义函数，具体语法如下：",{"type":18,"tag":57,"props":58,"children":62},"pre",{"code":59,"language":60,"meta":7,"className":61,"style":7},"def func(parameter1: type, parameter2: type = default,...) -> return_type:\n    \"\"\"\n    Docstrings\n    \"\"\"\n    func_body\n","python","language-python shiki shiki-themes github-dark",[63],{"type":18,"tag":48,"props":64,"children":65},{"__ignoreMap":7},[66,77,86,95,103],{"type":18,"tag":67,"props":68,"children":71},"span",{"class":69,"line":70},"line",1,[72],{"type":18,"tag":67,"props":73,"children":74},{},[75],{"type":28,"value":76},"def func(parameter1: type, parameter2: type = default,...) -> return_type:\n",{"type":18,"tag":67,"props":78,"children":80},{"class":69,"line":79},2,[81],{"type":18,"tag":67,"props":82,"children":83},{},[84],{"type":28,"value":85},"    \"\"\"\n",{"type":18,"tag":67,"props":87,"children":89},{"class":69,"line":88},3,[90],{"type":18,"tag":67,"props":91,"children":92},{},[93],{"type":28,"value":94},"    Docstrings\n",{"type":18,"tag":67,"props":96,"children":98},{"class":69,"line":97},4,[99],{"type":18,"tag":67,"props":100,"children":101},{},[102],{"type":28,"value":85},{"type":18,"tag":67,"props":104,"children":106},{"class":69,"line":105},5,[107],{"type":18,"tag":67,"props":108,"children":109},{},[110],{"type":28,"value":111},"    func_body\n",{"type":18,"tag":113,"props":114,"children":115},"ul",{},[116,128,171,182],{"type":18,"tag":117,"props":118,"children":119},"li",{},[120,126],{"type":18,"tag":48,"props":121,"children":123},{"className":122},[],[124],{"type":28,"value":125},"func",{"type":28,"value":127}," ：函数名，调用函数时使用",{"type":18,"tag":117,"props":129,"children":130},{},[131,137,139,145,147,153,155,161,163,169],{"type":18,"tag":48,"props":132,"children":134},{"className":133},[],[135],{"type":28,"value":136},"parameter",{"type":28,"value":138}," ：可选参数，当没有参数时，必须保留 ",{"type":18,"tag":48,"props":140,"children":142},{"className":141},[],[143],{"type":28,"value":144},"()",{"type":28,"value":146}," ，否则将会报错。若存在多个参数，每个参数间使用 ",{"type":18,"tag":48,"props":148,"children":150},{"className":149},[],[151],{"type":28,"value":152},",",{"type":28,"value":154}," 间隔。定义参数时可以通过 ",{"type":18,"tag":48,"props":156,"children":158},{"className":157},[],[159],{"type":28,"value":160},":",{"type":28,"value":162}," 指定参数的类型，也通过 ",{"type":18,"tag":48,"props":164,"children":166},{"className":165},[],[167],{"type":28,"value":168},"=",{"type":28,"value":170}," 赋值的方式指定默认值。定义函数时的参数称之为形式参数，调用函数时传递的参数称之为实际参数。",{"type":18,"tag":117,"props":172,"children":173},{},[174,180],{"type":18,"tag":48,"props":175,"children":177},{"className":176},[],[178],{"type":28,"value":179},"Docstrings",{"type":28,"value":181}," ：可选参数，用于为函数指定注释，也称文档字符串，其内容通常是说明该函数的功能、要传递的参数的作用。",{"type":18,"tag":117,"props":183,"children":184},{},[185,191],{"type":18,"tag":48,"props":186,"children":188},{"className":187},[],[189],{"type":28,"value":190},"func_body",{"type":28,"value":192}," ：函数体内容",{"type":18,"tag":36,"props":194,"children":195},{},[196],{"type":18,"tag":40,"props":197,"children":198},{},[199],{"type":28,"value":200},"如何调用函数的Docstrings",{"type":18,"tag":36,"props":202,"children":203},{},[204,206,212,214,220],{"type":28,"value":205},"在调用函数时，可以通过 ",{"type":18,"tag":48,"props":207,"children":209},{"className":208},[],[210],{"type":28,"value":211},"func.__doc__",{"type":28,"value":213}," 或者 ",{"type":18,"tag":48,"props":215,"children":217},{"className":216},[],[218],{"type":28,"value":219},"help(func)",{"type":28,"value":221}," 获取。代码示例如下：",{"type":18,"tag":57,"props":223,"children":225},{"code":224,"language":60,"meta":7,"className":61,"style":7},"def func():\n    \"\"\"\n    此处为文档字符串\n    帮助信息\n    \"\"\"\n    pass # 若定义函数无任何动作，则需要使用pass作为点位符或添加docstrings\n\nprint(func.__doc__)\nprint(help(func))\n",[226],{"type":18,"tag":48,"props":227,"children":228},{"__ignoreMap":7},[229,237,244,252,260,267,276,286,295],{"type":18,"tag":67,"props":230,"children":231},{"class":69,"line":70},[232],{"type":18,"tag":67,"props":233,"children":234},{},[235],{"type":28,"value":236},"def func():\n",{"type":18,"tag":67,"props":238,"children":239},{"class":69,"line":79},[240],{"type":18,"tag":67,"props":241,"children":242},{},[243],{"type":28,"value":85},{"type":18,"tag":67,"props":245,"children":246},{"class":69,"line":88},[247],{"type":18,"tag":67,"props":248,"children":249},{},[250],{"type":28,"value":251},"    此处为文档字符串\n",{"type":18,"tag":67,"props":253,"children":254},{"class":69,"line":97},[255],{"type":18,"tag":67,"props":256,"children":257},{},[258],{"type":28,"value":259},"    帮助信息\n",{"type":18,"tag":67,"props":261,"children":262},{"class":69,"line":105},[263],{"type":18,"tag":67,"props":264,"children":265},{},[266],{"type":28,"value":85},{"type":18,"tag":67,"props":268,"children":270},{"class":69,"line":269},6,[271],{"type":18,"tag":67,"props":272,"children":273},{},[274],{"type":28,"value":275},"    pass # 若定义函数无任何动作，则需要使用pass作为点位符或添加docstrings\n",{"type":18,"tag":67,"props":277,"children":279},{"class":69,"line":278},7,[280],{"type":18,"tag":67,"props":281,"children":283},{"emptyLinePlaceholder":282},true,[284],{"type":28,"value":285},"\n",{"type":18,"tag":67,"props":287,"children":289},{"class":69,"line":288},8,[290],{"type":18,"tag":67,"props":291,"children":292},{},[293],{"type":28,"value":294},"print(func.__doc__)\n",{"type":18,"tag":67,"props":296,"children":298},{"class":69,"line":297},9,[299],{"type":18,"tag":67,"props":300,"children":301},{},[302],{"type":28,"value":303},"print(help(func))\n",{"type":18,"tag":30,"props":305,"children":307},{"id":306},"调用函数",[308],{"type":28,"value":306},{"type":18,"tag":36,"props":310,"children":311},{},[312],{"type":28,"value":313},"调用函数也就是执行函数。如果把函数看作成工具，所以调用函数就是使用工具。调用函数的基本语法如下：",{"type":18,"tag":57,"props":315,"children":317},{"code":316,"language":60,"meta":7,"className":61,"style":7},"func(parameters)\n",[318],{"type":18,"tag":48,"props":319,"children":320},{"__ignoreMap":7},[321],{"type":18,"tag":67,"props":322,"children":323},{"class":69,"line":70},[324],{"type":18,"tag":67,"props":325,"children":326},{},[327],{"type":28,"value":316},{"type":18,"tag":113,"props":329,"children":330},{},[331,341],{"type":18,"tag":117,"props":332,"children":333},{},[334,339],{"type":18,"tag":48,"props":335,"children":337},{"className":336},[],[338],{"type":28,"value":125},{"type":28,"value":340},"：表示已定义好的函数名称",{"type":18,"tag":117,"props":342,"children":343},{},[344,350,352,357],{"type":18,"tag":48,"props":345,"children":347},{"className":346},[],[348],{"type":28,"value":349},"parameters",{"type":28,"value":351},"：可选参数，用于指定各个参数的值，若无参数，则保留 ",{"type":18,"tag":48,"props":353,"children":355},{"className":354},[],[356],{"type":28,"value":144},{"type":28,"value":358}," 即可。",{"type":18,"tag":23,"props":360,"children":362},{"id":361},"参数传递",[363],{"type":28,"value":361},{"type":18,"tag":30,"props":365,"children":367},{"id":366},"形参和实参",[368],{"type":28,"value":366},{"type":18,"tag":36,"props":370,"children":371},{},[372,377,379,384],{"type":18,"tag":40,"props":373,"children":374},{},[375],{"type":28,"value":376},"形式参数",{"type":28,"value":378},"：在定义函数时，函数名后面括号中的参数为“形式参数”。\n",{"type":18,"tag":40,"props":380,"children":381},{},[382],{"type":28,"value":383},"实际参数",{"type":28,"value":385},"：在调用一个函数时，函数名后面的括号中的参数为“实际参数”。",{"type":18,"tag":387,"props":388,"children":389},"blockquote",{},[390],{"type":18,"tag":36,"props":391,"children":392},{},[393],{"type":28,"value":394},"在进行值传递时，改变形式参数的值后，实际参数的值不改变；在进行引用传递时，改变形式参数的值后，实际参数的值也发生改变。",{"type":18,"tag":30,"props":396,"children":398},{"id":397},"位置参数",[399],{"type":28,"value":397},{"type":18,"tag":36,"props":401,"children":402},{},[403,408,410,416],{"type":18,"tag":40,"props":404,"children":405},{},[406],{"type":28,"value":407},"数量必须与定义时一致",{"type":28,"value":409},"：在调用函数时，指定的实际参数的数量必须与形式参数的数量一致，否则将会抛出",{"type":18,"tag":48,"props":411,"children":413},{"className":412},[],[414],{"type":28,"value":415},"TypeError",{"type":28,"value":417}," 异常信息，提示缺少必要参数。",{"type":18,"tag":36,"props":419,"children":420},{},[421,426],{"type":18,"tag":40,"props":422,"children":423},{},[424],{"type":28,"value":425},"位置必须与定义时一致",{"type":28,"value":427},"：指定的实际参数的位置必须与形式参数的位置一致，否则将会产生以下两种结果：",{"type":18,"tag":113,"props":429,"children":430},{},[431,443],{"type":18,"tag":117,"props":432,"children":433},{},[434,436,441],{"type":28,"value":435},"抛出 ",{"type":18,"tag":48,"props":437,"children":439},{"className":438},[],[440],{"type":28,"value":415},{"type":28,"value":442}," 异常信息",{"type":18,"tag":117,"props":444,"children":445},{},[446],{"type":28,"value":447},"产生的结果与预期不符",{"type":18,"tag":36,"props":449,"children":450},{},[451],{"type":28,"value":452},"举个实例帮助理解，首先我先定义一个函数，功能为输出姓名、年龄、身高：",{"type":18,"tag":57,"props":454,"children":456},{"code":455,"language":60,"meta":7,"className":61,"style":7},"def func(name: str,age: int, height: int):\n    \"\"\"\n    功能为输出姓名、年龄、身高\n    参数依次为name, age, height\n    \"\"\"\n    print(f\"当前用户的姓名是{name}，年龄是{age}，身高是{height}\")\n",[457],{"type":18,"tag":48,"props":458,"children":459},{"__ignoreMap":7},[460,468,475,483,491,498],{"type":18,"tag":67,"props":461,"children":462},{"class":69,"line":70},[463],{"type":18,"tag":67,"props":464,"children":465},{},[466],{"type":28,"value":467},"def func(name: str,age: int, height: int):\n",{"type":18,"tag":67,"props":469,"children":470},{"class":69,"line":79},[471],{"type":18,"tag":67,"props":472,"children":473},{},[474],{"type":28,"value":85},{"type":18,"tag":67,"props":476,"children":477},{"class":69,"line":88},[478],{"type":18,"tag":67,"props":479,"children":480},{},[481],{"type":28,"value":482},"    功能为输出姓名、年龄、身高\n",{"type":18,"tag":67,"props":484,"children":485},{"class":69,"line":97},[486],{"type":18,"tag":67,"props":487,"children":488},{},[489],{"type":28,"value":490},"    参数依次为name, age, height\n",{"type":18,"tag":67,"props":492,"children":493},{"class":69,"line":105},[494],{"type":18,"tag":67,"props":495,"children":496},{},[497],{"type":28,"value":85},{"type":18,"tag":67,"props":499,"children":500},{"class":69,"line":269},[501],{"type":18,"tag":67,"props":502,"children":503},{},[504],{"type":28,"value":505},"    print(f\"当前用户的姓名是{name}，年龄是{age}，身高是{height}\")\n",{"type":18,"tag":36,"props":507,"children":508},{},[509],{"type":28,"value":510},"现在我使用位置传参调用所定义的函数（其中包含错误的调用方式示例）：",{"type":18,"tag":57,"props":512,"children":514},{"code":513,"language":60,"meta":7,"className":61,"style":7},"name,age,height=\"路人甲\", 18, 180\n# 正确案例：\nfunc(name,age,height)\n# 错误示范1：数量不一致\nfunc(name,age)                 # 将抛出异常\n# 错误示范2：位置不一致 \nfunc(name,height,age)          # 结果与预期不符\n",[515],{"type":18,"tag":48,"props":516,"children":517},{"__ignoreMap":7},[518,526,534,542,550,558,566],{"type":18,"tag":67,"props":519,"children":520},{"class":69,"line":70},[521],{"type":18,"tag":67,"props":522,"children":523},{},[524],{"type":28,"value":525},"name,age,height=\"路人甲\", 18, 180\n",{"type":18,"tag":67,"props":527,"children":528},{"class":69,"line":79},[529],{"type":18,"tag":67,"props":530,"children":531},{},[532],{"type":28,"value":533},"# 正确案例：\n",{"type":18,"tag":67,"props":535,"children":536},{"class":69,"line":88},[537],{"type":18,"tag":67,"props":538,"children":539},{},[540],{"type":28,"value":541},"func(name,age,height)\n",{"type":18,"tag":67,"props":543,"children":544},{"class":69,"line":97},[545],{"type":18,"tag":67,"props":546,"children":547},{},[548],{"type":28,"value":549},"# 错误示范1：数量不一致\n",{"type":18,"tag":67,"props":551,"children":552},{"class":69,"line":105},[553],{"type":18,"tag":67,"props":554,"children":555},{},[556],{"type":28,"value":557},"func(name,age)                 # 将抛出异常\n",{"type":18,"tag":67,"props":559,"children":560},{"class":69,"line":269},[561],{"type":18,"tag":67,"props":562,"children":563},{},[564],{"type":28,"value":565},"# 错误示范2：位置不一致 \n",{"type":18,"tag":67,"props":567,"children":568},{"class":69,"line":278},[569],{"type":18,"tag":67,"props":570,"children":571},{},[572],{"type":28,"value":573},"func(name,height,age)          # 结果与预期不符\n",{"type":18,"tag":30,"props":575,"children":577},{"id":576},"关键字参数",[578],{"type":28,"value":576},{"type":18,"tag":36,"props":580,"children":581},{},[582],{"type":28,"value":583},"关键字参数是指使用形式参数的名字来确定输入的参数值。语法格式如下：",{"type":18,"tag":57,"props":585,"children":587},{"code":586,"language":60,"meta":7,"className":61,"style":7},"函数名称(形式参数名2 = 实际参数名2,形式参数名1 = 实际参数名1,...)\n",[588],{"type":18,"tag":48,"props":589,"children":590},{"__ignoreMap":7},[591],{"type":18,"tag":67,"props":592,"children":593},{"class":69,"line":70},[594],{"type":18,"tag":67,"props":595,"children":596},{},[597],{"type":28,"value":586},{"type":18,"tag":387,"props":599,"children":600},{},[601],{"type":18,"tag":36,"props":602,"children":603},{},[604],{"type":28,"value":605},"当使用关键字传参数时，参数的传递位置可以不用一致",{"type":18,"tag":30,"props":607,"children":609},{"id":608},"为参数设置默认值",[610],{"type":28,"value":608},{"type":18,"tag":36,"props":612,"children":613},{},[614],{"type":28,"value":615},"调用函数时若没指定某个参数，则程序将会报错。为解决这一问题，我们可以在定义函数时，直接指定形式参数的默认值，此时当没有输入参数时，可以直接使用定义函数时设置的默认值。示例代码如下：",{"type":18,"tag":57,"props":617,"children":619},{"code":618,"language":60,"meta":7,"className":61,"style":7},"def func(age: int, height: int, name: str = \"路人\"):\n    \"\"\"\n    功能为输出姓名、年龄、身高\n    参数依次为name, age, height\n    \"\"\"\n    print(f\"当前用户的姓名是{name}，年龄是{age}，身高是{height}\")\n",[620],{"type":18,"tag":48,"props":621,"children":622},{"__ignoreMap":7},[623,631,638,645,652,659],{"type":18,"tag":67,"props":624,"children":625},{"class":69,"line":70},[626],{"type":18,"tag":67,"props":627,"children":628},{},[629],{"type":28,"value":630},"def func(age: int, height: int, name: str = \"路人\"):\n",{"type":18,"tag":67,"props":632,"children":633},{"class":69,"line":79},[634],{"type":18,"tag":67,"props":635,"children":636},{},[637],{"type":28,"value":85},{"type":18,"tag":67,"props":639,"children":640},{"class":69,"line":88},[641],{"type":18,"tag":67,"props":642,"children":643},{},[644],{"type":28,"value":482},{"type":18,"tag":67,"props":646,"children":647},{"class":69,"line":97},[648],{"type":18,"tag":67,"props":649,"children":650},{},[651],{"type":28,"value":490},{"type":18,"tag":67,"props":653,"children":654},{"class":69,"line":105},[655],{"type":18,"tag":67,"props":656,"children":657},{},[658],{"type":28,"value":85},{"type":18,"tag":67,"props":660,"children":661},{"class":69,"line":269},[662],{"type":18,"tag":67,"props":663,"children":664},{},[665],{"type":28,"value":505},{"type":18,"tag":36,"props":667,"children":668},{},[669,671,677,679,685,687,692],{"type":28,"value":670},"此时我为",{"type":18,"tag":48,"props":672,"children":674},{"className":673},[],[675],{"type":28,"value":676},"name",{"type":28,"value":678},"形参定义了默认值",{"type":18,"tag":48,"props":680,"children":682},{"className":681},[],[683],{"type":28,"value":684},"路人",{"type":28,"value":686},"，所以我调用时可以不用传递",{"type":18,"tag":48,"props":688,"children":690},{"className":689},[],[691],{"type":28,"value":676},{"type":28,"value":693},"的参数，如下调用方式：",{"type":18,"tag":57,"props":695,"children":697},{"code":696,"language":60,"meta":7,"className":61,"style":7},"age,height=18, 180\n# 正确案例\nfunc(age,height)\n",[698],{"type":18,"tag":48,"props":699,"children":700},{"__ignoreMap":7},[701,709,717],{"type":18,"tag":67,"props":702,"children":703},{"class":69,"line":70},[704],{"type":18,"tag":67,"props":705,"children":706},{},[707],{"type":28,"value":708},"age,height=18, 180\n",{"type":18,"tag":67,"props":710,"children":711},{"class":69,"line":79},[712],{"type":18,"tag":67,"props":713,"children":714},{},[715],{"type":28,"value":716},"# 正确案例\n",{"type":18,"tag":67,"props":718,"children":719},{"class":69,"line":88},[720],{"type":18,"tag":67,"props":721,"children":722},{},[723],{"type":28,"value":724},"func(age,height)\n",{"type":18,"tag":387,"props":726,"children":727},{},[728],{"type":18,"tag":36,"props":729,"children":730},{},[731,733,739,741],{"type":28,"value":732},"在定义函数时，指定默认的形式参数必须在所有参数的末尾，否则将会产生语法错误。另外可以使用 ",{"type":18,"tag":48,"props":734,"children":736},{"className":735},[],[737],{"type":28,"value":738},"func.__defaults__",{"type":28,"value":740}," 查看函数的默认值参数的当前值，其结果是一个元组，如上示例调用该语句，将显示 ",{"type":18,"tag":48,"props":742,"children":744},{"className":743},[],[745],{"type":28,"value":746},"('路人',)",{"type":18,"tag":30,"props":748,"children":750},{"id":749},"可变参数",[751],{"type":28,"value":749},{"type":18,"tag":36,"props":753,"children":754},{},[755],{"type":28,"value":756},"可变参数也称可变长参数，即传入函数中的实际参数可以是任意个。定义可变参数存在两种形式：",{"type":18,"tag":113,"props":758,"children":759},{},[760,771],{"type":18,"tag":117,"props":761,"children":762},{},[763,769],{"type":18,"tag":48,"props":764,"children":766},{"className":765},[],[767],{"type":28,"value":768},"*parameter",{"type":28,"value":770},"：表示接受任意多个实际参数并将其放到一个元组中；",{"type":18,"tag":117,"props":772,"children":773},{},[774,780],{"type":18,"tag":48,"props":775,"children":777},{"className":776},[],[778],{"type":28,"value":779},"**parameter",{"type":28,"value":781},"：表示接受任意多个类似关键字参数一样显式赋值的实际参数，并将其放到一个字典中。",{"type":18,"tag":36,"props":783,"children":784},{},[785,787,792,794,800],{"type":28,"value":786},"在",{"type":18,"tag":48,"props":788,"children":790},{"className":789},[],[791],{"type":28,"value":768},{"type":28,"value":793},"进行传参时，若参数对象为列表，则可以直接在列表前添加 ",{"type":18,"tag":48,"props":795,"children":797},{"className":796},[],[798],{"type":28,"value":799},"*",{"type":28,"value":801}," 作为参数传入。如下示例：",{"type":18,"tag":57,"props":803,"children":805},{"code":804,"language":60,"meta":7,"className":61,"style":7},"lis = [1,2,3,4,5]\nfunc(*lis)        # func函数参数为可变参数\n",[806],{"type":18,"tag":48,"props":807,"children":808},{"__ignoreMap":7},[809,817],{"type":18,"tag":67,"props":810,"children":811},{"class":69,"line":70},[812],{"type":18,"tag":67,"props":813,"children":814},{},[815],{"type":28,"value":816},"lis = [1,2,3,4,5]\n",{"type":18,"tag":67,"props":818,"children":819},{"class":69,"line":79},[820],{"type":18,"tag":67,"props":821,"children":822},{},[823],{"type":28,"value":824},"func(*lis)        # func函数参数为可变参数\n",{"type":18,"tag":36,"props":826,"children":827},{},[828,829,834,836,842],{"type":28,"value":786},{"type":18,"tag":48,"props":830,"children":832},{"className":831},[],[833],{"type":28,"value":779},{"type":28,"value":835},"进行传参时，若参数对象为字典，则可以直接在字典前添加 ",{"type":18,"tag":48,"props":837,"children":839},{"className":838},[],[840],{"type":28,"value":841},"**",{"type":28,"value":801},{"type":18,"tag":57,"props":844,"children":846},{"code":845,"language":60,"meta":7,"className":61,"style":7},"def func(**dic):\n    print()\n    for key,value in dic.items():\n        print(\"[\"+key+\"]\"+\"的ASCII值是\"+str(value))\n\ndic = {'a':97,'b':98,'c':99}\nfunc(**dic)        # func函数参数为可变参数\n",[847],{"type":18,"tag":48,"props":848,"children":849},{"__ignoreMap":7},[850,858,866,874,882,889,897],{"type":18,"tag":67,"props":851,"children":852},{"class":69,"line":70},[853],{"type":18,"tag":67,"props":854,"children":855},{},[856],{"type":28,"value":857},"def func(**dic):\n",{"type":18,"tag":67,"props":859,"children":860},{"class":69,"line":79},[861],{"type":18,"tag":67,"props":862,"children":863},{},[864],{"type":28,"value":865},"    print()\n",{"type":18,"tag":67,"props":867,"children":868},{"class":69,"line":88},[869],{"type":18,"tag":67,"props":870,"children":871},{},[872],{"type":28,"value":873},"    for key,value in dic.items():\n",{"type":18,"tag":67,"props":875,"children":876},{"class":69,"line":97},[877],{"type":18,"tag":67,"props":878,"children":879},{},[880],{"type":28,"value":881},"        print(\"[\"+key+\"]\"+\"的ASCII值是\"+str(value))\n",{"type":18,"tag":67,"props":883,"children":884},{"class":69,"line":105},[885],{"type":18,"tag":67,"props":886,"children":887},{"emptyLinePlaceholder":282},[888],{"type":28,"value":285},{"type":18,"tag":67,"props":890,"children":891},{"class":69,"line":269},[892],{"type":18,"tag":67,"props":893,"children":894},{},[895],{"type":28,"value":896},"dic = {'a':97,'b':98,'c':99}\n",{"type":18,"tag":67,"props":898,"children":899},{"class":69,"line":278},[900],{"type":18,"tag":67,"props":901,"children":902},{},[903],{"type":28,"value":904},"func(**dic)        # func函数参数为可变参数\n",{"type":18,"tag":36,"props":906,"children":907},{},[908],{"type":28,"value":909},"也可以使用类似关键字参数一样显式赋值的实际参数进行传参，如下示例：",{"type":18,"tag":57,"props":911,"children":913},{"code":912,"language":60,"meta":7,"className":61,"style":7},"func(A=65,B=66,C=67)\n",[914],{"type":18,"tag":48,"props":915,"children":916},{"__ignoreMap":7},[917],{"type":18,"tag":67,"props":918,"children":919},{"class":69,"line":70},[920],{"type":18,"tag":67,"props":921,"children":922},{},[923],{"type":28,"value":912},{"type":18,"tag":36,"props":925,"children":926},{},[927],{"type":28,"value":928},"上述两个案例的执行结果为：",{"type":18,"tag":57,"props":930,"children":932},{"code":931},"[a]的ASCII值是97\n[b]的ASCII值是98\n[c]的ASCII值是99\n\n[A]的ASCII值是65\n[B]的ASCII值是66\n[C]的ASCII值是67\n",[933],{"type":18,"tag":48,"props":934,"children":935},{"__ignoreMap":7},[936],{"type":28,"value":931},{"type":18,"tag":30,"props":938,"children":940},{"id":939},"类型联合运算符",[941],{"type":28,"value":939},{"type":18,"tag":36,"props":943,"children":944},{},[945],{"type":28,"value":946},"Python提供了一些方法可以将某些部分设置为静态，如编程时为函数参数指定某种数据类型，示例代码如下：",{"type":18,"tag":57,"props":948,"children":950},{"code":949,"language":60,"meta":7,"className":61,"style":7},"def func(num: int) -> int:\n    return num**2\n",[951],{"type":18,"tag":48,"props":952,"children":953},{"__ignoreMap":7},[954,962],{"type":18,"tag":67,"props":955,"children":956},{"class":69,"line":70},[957],{"type":18,"tag":67,"props":958,"children":959},{},[960],{"type":28,"value":961},"def func(num: int) -> int:\n",{"type":18,"tag":67,"props":963,"children":964},{"class":69,"line":79},[965],{"type":18,"tag":67,"props":966,"children":967},{},[968],{"type":28,"value":969},"    return num**2\n",{"type":18,"tag":36,"props":971,"children":972},{},[973],{"type":28,"value":974},"设置的语法格式如下所示：",{"type":18,"tag":57,"props":976,"children":978},{"code":977},"def 函数名(参数: type) -> type: # 指定返回值类型\n",[979],{"type":18,"tag":48,"props":980,"children":981},{"__ignoreMap":7},[982],{"type":28,"value":977},{"type":18,"tag":387,"props":984,"children":985},{},[986],{"type":18,"tag":36,"props":987,"children":988},{},[989,991,997],{"type":28,"value":990},"若是多种数据类型，类型之间可以使用 ",{"type":18,"tag":48,"props":992,"children":994},{"className":993},[],[995],{"type":28,"value":996},"|",{"type":28,"value":998}," 隔开",{"type":18,"tag":23,"props":1000,"children":1002},{"id":1001},"返回值",[1003],{"type":28,"value":1001},{"type":18,"tag":36,"props":1005,"children":1006},{},[1007,1009,1015,1017,1022],{"type":28,"value":1008},"Python中在函数体内使用 ",{"type":18,"tag":48,"props":1010,"children":1012},{"className":1011},[],[1013],{"type":28,"value":1014},"return",{"type":28,"value":1016}," 语句为函数指定返回值。返回值类型任意，位置任意，但是当程序一旦执行至 ",{"type":18,"tag":48,"props":1018,"children":1020},{"className":1019},[],[1021],{"type":28,"value":1014},{"type":28,"value":1023}," 语句将会立即结束函数的执行。代码示例如下：",{"type":18,"tag":57,"props":1025,"children":1027},{"code":1026,"language":60,"meta":7,"className":61,"style":7},"def func(n):\n    if n == 1:\n        return \"success\"\n    else:\n        return \"fial\"\n\nfunc(0)  # 当这样写时func函数的返回值未被接受，该指令无效\na = func(1) # 正确写法，成功接受到函数的返回值\nprint(a)\n",[1028],{"type":18,"tag":48,"props":1029,"children":1030},{"__ignoreMap":7},[1031,1039,1047,1055,1063,1071,1078,1086,1094],{"type":18,"tag":67,"props":1032,"children":1033},{"class":69,"line":70},[1034],{"type":18,"tag":67,"props":1035,"children":1036},{},[1037],{"type":28,"value":1038},"def func(n):\n",{"type":18,"tag":67,"props":1040,"children":1041},{"class":69,"line":79},[1042],{"type":18,"tag":67,"props":1043,"children":1044},{},[1045],{"type":28,"value":1046},"    if n == 1:\n",{"type":18,"tag":67,"props":1048,"children":1049},{"class":69,"line":88},[1050],{"type":18,"tag":67,"props":1051,"children":1052},{},[1053],{"type":28,"value":1054},"        return \"success\"\n",{"type":18,"tag":67,"props":1056,"children":1057},{"class":69,"line":97},[1058],{"type":18,"tag":67,"props":1059,"children":1060},{},[1061],{"type":28,"value":1062},"    else:\n",{"type":18,"tag":67,"props":1064,"children":1065},{"class":69,"line":105},[1066],{"type":18,"tag":67,"props":1067,"children":1068},{},[1069],{"type":28,"value":1070},"        return \"fial\"\n",{"type":18,"tag":67,"props":1072,"children":1073},{"class":69,"line":269},[1074],{"type":18,"tag":67,"props":1075,"children":1076},{"emptyLinePlaceholder":282},[1077],{"type":28,"value":285},{"type":18,"tag":67,"props":1079,"children":1080},{"class":69,"line":278},[1081],{"type":18,"tag":67,"props":1082,"children":1083},{},[1084],{"type":28,"value":1085},"func(0)  # 当这样写时func函数的返回值未被接受，该指令无效\n",{"type":18,"tag":67,"props":1087,"children":1088},{"class":69,"line":288},[1089],{"type":18,"tag":67,"props":1090,"children":1091},{},[1092],{"type":28,"value":1093},"a = func(1) # 正确写法，成功接受到函数的返回值\n",{"type":18,"tag":67,"props":1095,"children":1096},{"class":69,"line":297},[1097],{"type":18,"tag":67,"props":1098,"children":1099},{},[1100],{"type":28,"value":1101},"print(a)\n",{"type":18,"tag":23,"props":1103,"children":1105},{"id":1104},"变量的作用域",[1106],{"type":28,"value":1104},{"type":18,"tag":30,"props":1108,"children":1110},{"id":1109},"局部变量",[1111],{"type":28,"value":1109},{"type":18,"tag":36,"props":1113,"children":1114},{},[1115],{"type":28,"value":1116},"局部变量是指在函数内部定义和使用的变量，它只在函数内部生效，在函数运行之前或者之后，函数内部定义的所有变量名都不存在。",{"type":18,"tag":30,"props":1118,"children":1120},{"id":1119},"全局变量",[1121],{"type":28,"value":1119},{"type":18,"tag":36,"props":1123,"children":1124},{},[1125,1127,1133],{"type":28,"value":1126},"全局变量可以作用于函数的内外部。如果变量是在函数外部定义的，那么该变量称为全局变量。在函数内部定义的变量，在使用",{"type":18,"tag":48,"props":1128,"children":1130},{"className":1129},[],[1131],{"type":28,"value":1132},"global",{"type":28,"value":1134},"关键字进行修饰后将变为全局变量，该变量将不受局部变量的约束，还可以在函数内部对其进行修改。语法结构为：",{"type":18,"tag":57,"props":1136,"children":1138},{"code":1137,"language":60,"meta":7,"className":61,"style":7},"global 变量名\n",[1139],{"type":18,"tag":48,"props":1140,"children":1141},{"__ignoreMap":7},[1142],{"type":18,"tag":67,"props":1143,"children":1144},{"class":69,"line":70},[1145],{"type":18,"tag":67,"props":1146,"children":1147},{},[1148],{"type":28,"value":1137},{"type":18,"tag":387,"props":1150,"children":1151},{},[1152],{"type":18,"tag":36,"props":1153,"children":1154},{},[1155],{"type":28,"value":1156},"当局部变量与全局变量重名时，对函数内部的变量进行赋值后，不影响函数外部的变量。",{"type":18,"tag":23,"props":1158,"children":1160},{"id":1159},"匿名函数",[1161],{"type":28,"value":1159},{"type":18,"tag":36,"props":1163,"children":1164},{},[1165,1167,1173],{"type":28,"value":1166},"匿名函数是指无名字的函数，通常情况下，该函数只是用一次。使用",{"type":18,"tag":48,"props":1168,"children":1170},{"className":1169},[],[1171],{"type":28,"value":1172},"lambda",{"type":28,"value":1174},"表达式可以创建匿名函数，其语法格式如下：",{"type":18,"tag":57,"props":1176,"children":1178},{"code":1177,"language":60,"meta":7,"className":61,"style":7},"result = lambda [arg1 [,arg2,...,argn]]:expression\n",[1179],{"type":18,"tag":48,"props":1180,"children":1181},{"__ignoreMap":7},[1182],{"type":18,"tag":67,"props":1183,"children":1184},{"class":69,"line":70},[1185],{"type":18,"tag":67,"props":1186,"children":1187},{},[1188],{"type":28,"value":1177},{"type":18,"tag":113,"props":1190,"children":1191},{},[1192,1210,1221],{"type":18,"tag":117,"props":1193,"children":1194},{},[1195,1201,1203,1208],{"type":18,"tag":48,"props":1196,"children":1198},{"className":1197},[],[1199],{"type":28,"value":1200},"result",{"type":28,"value":1202}," ：用于调用 ",{"type":18,"tag":48,"props":1204,"children":1206},{"className":1205},[],[1207],{"type":28,"value":1172},{"type":28,"value":1209}," 表达式",{"type":18,"tag":117,"props":1211,"children":1212},{},[1213,1219],{"type":18,"tag":48,"props":1214,"children":1216},{"className":1215},[],[1217],{"type":28,"value":1218},"[arg1 [,arg2,...,argn]]",{"type":28,"value":1220},"：可选参数，用于指定要传递的参数列表",{"type":18,"tag":117,"props":1222,"children":1223},{},[1224,1230],{"type":18,"tag":48,"props":1225,"children":1227},{"className":1226},[],[1228],{"type":28,"value":1229},"expression",{"type":28,"value":1231},"：必选参数，用于指定一个实现具体功能的表达式",{"type":18,"tag":387,"props":1233,"children":1234},{},[1235],{"type":18,"tag":36,"props":1236,"children":1237},{},[1238,1243],{"type":18,"tag":48,"props":1239,"children":1241},{"className":1240},[],[1242],{"type":28,"value":1172},{"type":28,"value":1244}," 参数可以存在多个，但表达式只能有一个，即只能返回一个值，而且不能出现其他非表达式语句",{"type":18,"tag":1246,"props":1247,"children":1248},"style",{},[1249],{"type":28,"value":1250},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":79,"depth":79,"links":1252},[1253,1254,1255,1256,1257],{"id":25,"depth":88,"text":25},{"id":361,"depth":88,"text":361},{"id":1001,"depth":88,"text":1001},{"id":1104,"depth":88,"text":1104},{"id":1159,"depth":88,"text":1159},"markdown","content:blog:2025-07-07-python-函数.md","content","blog\u002F2025-07-07-python-函数.md","blog\u002F2025-07-07-python-函数","md",1780801018435]