python中的自定义函数

1格式:

deffunctionName(参数列表):

方法体

例子1:

>>>def greet_user():
        print(“hello”)
>>>greet_user()
hello

例子2:

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

2传递实参

鉴于函数定义中可能包含多个形参,因此在函数调用中也可能包含多个实参。向函数中传递实参的方式很多,可以使用位置实参,这要求实参的顺序和形参的顺序相同;也可以使用关键字形参,其中的每个实参都有变量名和值组成;还可以使用列表和字典。

(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将使用指定实参,否则使用默认值。

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/venus
earth.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 Limburger
It‘s very runny,sir.
It‘s reallyvery, VERY runny, sir.
----------------------------------------
client : JohnCleese
shopkeeper :Michael Palin
sketch : 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 + b

Lambda 函数可以用于任何函数对象。他们在语法上限于单个表达式。语义上,他们都只是正常的函数定义的语法糖。嵌套的函数定义类似,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 ‘ +eggs
s=f(‘abc’)
print(s)

结果:

>>>
=========== RESTART:D:/python/pythonLearning/KeywordArguments.py ===========
Annotations: {‘ham‘: <class ‘str‘>,‘eggs‘: <class ‘str‘>, ‘return‘: <class ‘str‘>}
Arguments: abc eggs2
abc and eggs2
>>>

函数注解是完全可选的元数据信息,它告诉人们这个函数返回的类型应该是什么类型,函数形参应该是什么类型。但是传入其他类型可能会报错,当然,这并非绝对,只不过是开发者给调用者的一个建议。上面的例子中,即使实参传入数字也是不会报错的。(这段在python3.6帮助文档上的说明看得我很头疼,比较乱,我领会精神写的)

注解存储在 __annotations__ 属性中,以字典的形式存在,对其他部分的功能没有任何影响。

参数注释:在参数名称的后面加一个冒号,其后是期待实参的类型。

返回值注解:函数的参数列表外面,冒号前面,加上一个 –>,后面是期待返回的类型。

时间: 2024-08-03 09:20:50

python中的自定义函数的相关文章

Python中关于Lambda函数的使用总结

lambda表达式是一种匿名函数,对应python中的自定义函数def,是定义某个函数时比较高级的一种写法.作为python初学者,本文整理了lambda的一些基本用法和特点. lambda和def的对应关系 定义func函数,计算给定数x的平方 def func(x): return x*x 等价于 func = lambda x: x*x 其中func是函数名,x是输入参数,x*x是输出结果 输入参数可以有多个,可以接收不定参数如*args或者**kwargs. f = lambda x,

python中有趣的函数

filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回: >>> def f(x): return x % 2 != 0 and x % 3 != 0 >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] >>> def f

Smarty中的自定义函数(二)复选框、下拉列表、单选框

接Smarty中的自定义函数(一) 7.html_checkboxes 复选框 [php] view plain copy print? $ssss1 = array(2,3,4); $ssss2 = array('学习','工作','生活'); $ssss3 = array(2,4); $smarty->assign('ssss1',$ssss1); $smarty->assign('ssss2',$ssss2); $smarty->assign('ssss3',$ssss3); $s

举例详解Python中的split()函数的使用方法

这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)os.path.split():按照路径将文件名和路径分割开 一.函数说明1.split()函数语法:str.

Python中的getattr()函数详解:

Python中的getattr()函数详解: getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception i

python中的map()函数

MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下. 文档中的介绍在这里: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that man

python中的生成器函数是如何工作的?

以下内容基于python3.4 1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函数相关的栈帧,则必须在调用这个函数且这个函数尚未返回时获取,可能通过inspect模块的currentframe()函数获取当前栈帧. 栈帧对象中的3个常用的属性: f_back : 调用栈的上一级栈帧 f_code: 栈帧对应的c f_locals: 用在当前栈帧时的局部变量; 比如: >>&g

python进阶一(函数式编程)【2-2 python中的map函数】

2-2 python中的map()函数 python中map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 原文地址:https://www.cnblogs.com/ucasljq/p/11609544.html

python进阶一(函数式编程)【2-3 python中的reduce函数】

2-3 python中的reduce函数 python中reduce()函数 reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值. 例如,编写一个f函数,接收x和y,返回x和y的和: 1 def f(x, y): 2 return x + y 调用 reduce(f, [