Python自定义函数基础

您所在的位置:网站首页 python自定义函数怎么用 Python自定义函数基础

Python自定义函数基础

2022-05-07 05:35| 来源: 网络整理| 查看: 265

1 格式:    def functionName(参数列表):                  方法体例子1:>>>def greet_user():         print(“hello”) >>>greet_user() hello

 例子2:>>>def greet_user(username): #username形参       print(“hello,”+ username+“!”) >>>greet_user(“zhangsan”)#zhangsan 实参 hello, zhangsan!

鉴于函数定义中可能包含多个形参,因此在函数调用中也可能包含多个实参。向函数中传递实参的方式很多,可以使用位置实参,这要求实参的顺序和形参的顺序相同;也可以使用关键字形参,其中的每个实参都有变量名和值组成;还可以使用列表和字典。                                                      (1)位置实参        要求实参的顺序和形参的顺序相同        例子:        def describe_pet(animal_type,pet_name):                   print(“\n I have a”+animal_type+”.”)                   print(“My ”+animal_type+”’sname is ”+pet_name+”.”)  describe_pet(‘hamster’,’harry’)

当两个参数的位置交换后,表达的意思就会出现错误。这种传参方式应该是最好理解的,大部分语言都是这种传参方式。(2)默认参数编写函数时,可给每个形参指定默认值。在调用函数中给形参提供了实参时,Python将使用指定实参,否则使用默认值。123 def describe_pet(pet_name,animal_type=‘dog’):     print(“\n I have a”+animal_type+”.”)     print(“My ”+animal_type+”’sname is ”+pet_name+”.”)

注意:在使用默认参数时,在形参列表中必须先列出没有默认值的形参,再列出有默认值的形参。当然,默认值不一定是字符串,数字等,同时也支持各种序列。def f(a, L=[]):     L.append(a)     return L   print(f(1)) print(f(2)) print(f(3))

输出结果为[1][1, 2][1, 2, 3] 如果你不想调用之间共享的默认设置,你可以这样写def f(a, L=None):     if L is None:         L = []     L.append(a)     return L

感觉有点像Java中单例模式中的懒汉式,虽然不能这么比。 (3)关键字实参        关键字实参是传递给函数的 名称-值对。你直接在实参中将名称和值关联起来,因此向函数传递实参时不会混淆def parrot(voltage, state='a stiff', action='voom',type='Norwegian Blue'):     print("--This parrot wouldn't", action, end=' ')     print("ifyou put", voltage, "volts through it.")     print("--Lovely plumage, the", type)     print("--It's", state, "!")   parrot(1000)                                          # 1positional argument parrot(voltage=1000)                                  # 1 keywordargument parrot(voltage=1000000,action='VOOOOOM')            # 2 keywordarguments parrot(action='VOOOOOM',voltage=1000000)            # 2 keywordarguments parrot('amillion', 'bereft of life', 'jump')        # 3 positional arguments parrot('athousand', state='pushing up the daisies') # 1 positional, 1 keyword

执行后的结果>>>===========RESTART: D:/python/pythonLearning/KeywordArguments.py ===========-- This parrotwouldn't voom if you put 1000 volts through it.-- Lovelyplumage, the Norwegian Blue-- It's a stiff!-- This parrotwouldn't voom if you put 1000 volts through it.-- Lovelyplumage, the Norwegian Blue-- It's a stiff!-- This parrotwouldn't VOOOOOM if you put 1000000 volts through it.-- Lovelyplumage, the Norwegian Blue-- It's a stiff!-- This parrotwouldn't VOOOOOM if you put 1000000 volts through it.-- Lovelyplumage, the Norwegian Blue-- It's a stiff!-- This parrotwouldn't jump if you put a million volts through it.-- Lovelyplumage, the Norwegian Blue-- It's bereftof life !-- This parrotwouldn't voom if you put a thousand volts through it.-- Lovelyplumage, the Norwegian Blue-- It's pushingup the daisies ! 但是下面的调用方式会报错:parrot()                    # 必须的参数没有填入parrot(voltage=5.0, 'dead')  # 关键字传参后面的一定也是关键字传参parrot(110, voltage=220)    # 同一个参数传了不同的实参parrot(actor='John Cleese')  # 函数定义中没有这个形参。关键字实参后面不能再有位置实参。当必填参数没有时,会报错。当必填参数以位置实参的方式传递时,默认参数可以按照关键字实参的方式传递,也可以以位置实参的方式传递,而且此时,必填参数一定在默认参数前面。当必填参数以关键字实参方式传递的时候,默认参数传递的时候也一定是以关键字实参方式传递,而且此时与位置无关。关键字实参的关键字必须是形参中有的,否则会报错。(4)任意多数目的参数“*”最不常使用的选项是指定可以使用任意数量的参数调用的函数。这些参数将被裹在一个元组中。在可变数目的参数之前, 零个或更多的正常参数可能会发生。def write_multiple_items(file, separator, *args):   file.write(separator.join(args))

通常情况下,可变参数将在参数列表中的最后(含有默认参数的时候,默认参数在可变参数后面。另外,传入字典参数也在可变参数之后,因为字典参数也是一种可变参数)。在可变参数后面的参数,只能以关键字实参的方式传递,而不能使用位置实参传递方式。def concat(*args, sep="/"):     return sep.join(args)   print(concat("earth", "mars", "venus")) print(concat("earth", "mars", "venus",sep="."))

 

结果为:>>>===========RESTART: D:/python/pythonLearning/KeywordArguments.py ===========earth/mars/venusearth.mars.venus(5)需要字典类型的参数“**”下面例子中**keywords代表传入字典类型的参数,而且*一定要在**之前。def cheeseshop(kind, *arguments, **keywords):     print("--Do you have any", kind, "?")     print("--I'm sorry, we're all out of", kind)     for arg inarguments:         print(arg)   print("-" * 40)     keys =sorted(keywords.keys())     for kw in keys:         print(kw,":", keywords[kw])

 可以这样调用    cheeseshop("Limburger", "It's very runny, sir.",

          "It's really very, VERY runny, sir.",          shopkeeper="Michael Palin",          client="John Cleese",          sketch="Cheese Shop Sketch")当然结果如下:        >>>===========RESTART: D:/python/pythonLearning/KeywordArguments.py ===========-- Do you haveany Limburger ?-- I'm sorry,we're all out of LimburgerIt's very runny,sir.It's reallyvery, VERY runny, sir.----------------------------------------client : JohnCleeseshopkeeper :Michael Palinsketch : CheeseShop Sketch(6)Unpacking Argument Lists(python3.6帮助文档上用的这个词,必应翻译给的翻译是开箱的参数列表,我感觉应该是参数列表的拆包,不够下面还是用了开箱这个词,大家理解下)        这是与之前把数据组合成list相反的操作,即把list中的数据进行开箱操作,把里面的数据全部取出来使用,有点不好理解,看个例子吧 >>> list(range(3,6)) [3, 4, 5] >>> args=[3,6] >>> list(range(*args)) [3, 4, 5] >>> args=(3,6) >>> list(range(*args)) [3, 4, 5]

>>>这里把args中的数据进行开箱操作,把里面的数据作为range的参数列表.很显然,这个开箱操作对于元组也是适用的. 另外,也可以对字典执行开箱操作,需要的是两个*号即可.def parrot(voltage, state='a stiff',action='voom'):   print("-- This parrot wouldn't", action, end=' ')   print("if you put", voltage, "volts through it.",end=' ')   print("E's", state, "!")

 d = {"voltage": "fourmillion", "state": "bleedin' demised","action": "VOOM"}parrot(**d) 结果为:>>>=========== RESTART:D:/python/pythonLearning/KeywordArguments.py ===========-- This parrot wouldn't VOOM if you putfour million volts through it. E's bleedin' demised !>>>

(7)lambda表达式  可以使用 lambda 关键字创建小的匿名函数。此函数返回其两个参数的总和        lambda a, b:a + bLambda 函数可以用于任何函数对象。他们在语法上限于单个表达式。语义上,他们都只是正常的函数定义的语法糖。嵌套的函数定义类似,lambda 函数可以从包含范围引用变量。(之后的文章会详细讲解lambda表达式的相关操作)def make_incrementor1(n):    return lambdax:x+n f=make_incrementor1(42) print(f(1)) >>>=========== RESTART:D:/python/pythonLearning/KeywordArguments.py ===========43>>> (8) Function Annotations函数注解首先是一个简单的例子def f(ham:str,eggs:str='eggs2')->str:  print("Annotations:",f.__annotations__)  print("Arguments:",ham,eggs)  return str(ham)+' and ' +eggss=f(‘abc’)print(s)结果:>>>=========== RESTART:D:/python/pythonLearning/KeywordArguments.py ===========Annotations: {'ham': ,'eggs': , 'return': }Arguments: abc eggs2abc and eggs2>>>   函数注解是完全可选的元数据信息,它告诉人们这个函数返回的类型应该是什么类型,函数形参应该是什么类型。但是传入其他类型可能会报错,当然,这并非绝对,只不过是开发者给调用者的一个建议。上面的例子中,即使实参传入数字也是不会报错的。(这段在python3.6帮助文档上的说明看得我很头疼,比较乱,我领会精神写的) 注解存储在 __annotations__ 属性中,以字典的形式存在,对其他部分的功能没有任何影响。参数注释:在参数名称的后面加一个冒号,其后是期待实参的类型。返回值注解:函数的参数列表外面,冒号前面,加上一个 –>,后面是期待返回的类型。

下面关于Python的文章您也可能喜欢,不妨参考下:

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] 下载见 http://www.linuxidc.com/Linux/2013-06/85425.htm

零基础如何入门Python http://www.linuxidc.com/Linux/2016-10/136485.htm

Ubuntu 14.04安装Python 3.3.5  http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS 6.5 脚本自动化装 Python2.6升级2.7  http://www.linuxidc.com/Linux/2017-02/140406.htm

CentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

Ubuntu 14.04下Python数据处理环境搭建  http://www.linuxidc.com/Linux/2017-01/139568.htm

Python Paramiko模块安装和使用  http://www.linuxidc.com/Linux/2017-01/139973.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

在CentOS 6.5上安装Python2.7  http://www.linuxidc.com/Linux/2016-10/136206.htm

Ubuntu 14.04 LTS下编译安装Open Babel和Python语言绑定 http://www.linuxidc.com/Linux/2017-01/139569.htm

Python常见数据结构整理  http://www.linuxidc.com/Linux/2017-02/140613.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/142559.htm

linux


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3