可使用内置函数callable判断某个对象是否可调用
>>> import math
>>> x = 1
>>> y = math.sqrt
>>> callable(x)
False
>>> callable(y)
True
 
用def定义函数
def hello(name):
    return 'Hello, ' + name + '!'
>>> print(hello('world'))
Hello, world!
>>> print(hello('Gumby'))
Hello, Gumby!
 
放在函数开头的字符串称为文档字符串(docstring) ,将作为函数的一部分存储起来
def square(x):
    'Calculates the square of the number x.'
    return x * x
>>> square.__doc__
'Calculates the square of the number x.'
 
可用内置函数help获取有关函数的信息,其中包含函数的文档字符串
>>> help(square)
Help on function square in module __main__:
square(x)
Calculates the square of the number x.
 
有些函数什么都不返回, 什么都不返回的函数不包含return语句,或者包含return语句,但没
有在return后面指定值。
def test():
    print('This is printed')
    return
    print('This is not')
>>> x = test()
This is printed
这里的return类似循环的break,只是跳出的是函数
>>> x
>>>
>>> print(x)
None
所有的函数都返回值。如果你没有告诉它们该返回什么,将返回None。
 
关键字参数:有时候,参数的排列顺序可能难以记住,尤其是参数很多时。为了简化调用工作,可指定参
数的名称
def hello_1(greeting, name):
    print('{}, {}!'.format(greeting, name))
>>> hello_1(greeting='Hello', name='world')
Hello, world!
>>> hello_1(name='world', greeting='Hello')
Hello, world!
关键字参数可以指定默认值
def hello_3(greeting='Hello', name='world'):
print('{}, {}!'.format(greeting, name))
>>> hello_3()
Hello, world!
>>> hello_3('Greetings')
Greetings, world!
>>> hello_3('Greetings', 'universe')
Greetings, universe!
 
下边的函数要求必须指定姓名,而问候语和标点是可选的
ef hello_4(name, greeting='Hello', punctuation='!'):
print('{}, {}{}'.format(greeting, name, punctuation))
 
>>> hello_4('Mars')
Hello, Mars!
>>> hello_4('Mars', 'Howdy')
Howdy, Mars!
>>> hello_4('Mars', 'Howdy', '...')
Howdy, Mars...
>>> hello_4('Mars', punctuation='.')
Hello, Mars.
>>> hello_4('Mars', greeting='Top of the morning to ya')
Top of the morning to ya, Mars!
>>> hello_4()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hello_4() missing 1 required positional argument: 'name'
 
如果给参数name也指定了默认值,最后一个调用就不会引发异常。
 
 
 

Python-14-抽象及关键字参数的更多相关文章

  1. Python——函数的命名关键字参数

    命名关键字参数 对于关键字参数,函数的调用者可以传入任意不受限制的关键字参数.至于到底传入了哪些,就需要在函数内部通过kw检查. 仍以person()函数为例,我们希望检查是否有city和job参数: ...

  2. python函数 位置参数,关键字参数,可变参数优先级

    def fun(arg,args=1,*arg,**keywords): python 一共有这四类参数,第一类最常见,不用多说,第二类,关键字参数,python能通过关键字找到参数,python函数 ...

  3. Python——函数中的关键字参数

    关键字参数 可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple.而关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict.请看 ...

  4. python函数中的关键字参数

    关键字参数: 就是在形式参数中必须要提供”传递参数名=传递参数值” 位置参数:  仅仅只有参数名 特点:1.位置参数只能出现在关键字参数之前,不管是在行参还是实参中. 2.关键字参数在调用时(实参)中 ...

  5. [python 函数学习篇] 关键字参数

    函数可以通过 关键字参数 的形式来调用,形如 keyword = value .例如,以下的函数: def parrot(voltage, state='a stiff', action='voom' ...

  6. python的位置参数、关键字参数、收集参数,关键字收集参数混合调用问题

    参数混合调用顺序用法: 函数中参数顺序为:普通参数,收集参数,关键字参数,关键字收集参数,其顺序不能颠倒,颠倒会报错. 普通参数.关键字参数可以有n个,对量没有具体要求,收集参数和关键字收集参数要么没 ...

  7. python函数传入参数(默认参数、可变长度参数、关键字参数)

    1.python中默认缺省参数----定义默认参数要牢记一点:默认参数必须指向不变对象! 1 def foo(a,b=1): 2 print a,b 3 4 foo(2) #2 1 5 foo(3,1 ...

  8. python学习笔记 可变参数关键字参数**kw相关学习

    在Python中可以定义可变参数,顾名思义,可变参数就是传入参数是可变的.可以是任意个,以一个简单的数学编程为例,计算 sum = a * a + b * b + .....z * z 函数定义可以如 ...

  9. Python星号*与**用法分析 What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 必选参数 默认参数 可变参数 关键字参数

    python中*号**的区别 - CSDN博客 https://blog.csdn.net/qq_26815677/article/details/78091452 定义可变参数和定义 list 或 ...

  10. python语言(四)关键字参数、内置函数、导入第三方模块、OS模块、时间模块

    一.可变参数 定义函数时,有时候我们不确定调用的时候会传递多少个参数(不传参也可以).此时,可用包裹(packing)位置参数(*args),或者包裹关键字参数(**kwargs),来进行参数传递,会 ...

随机推荐

  1. 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5 ,PHP5.6)支持。

    本人预装了一个wamp的集成环境(Apache 2.4.9 + PHP 5.5.12 + mysql 5.6.17),今天在wamp环境下 添加PHP多版本 (PHP 5.5.30).中间两个过程,1 ...

  2. SDUT OJ 之 连通分量个数 (dfs)

    数据结构实验:连通分量个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  在无向图中,如果从顶点vi到顶点vj有路径,则称vi ...

  3. .NET中Eval()方法大全

    <%# Bind("Subject") %> //绑定字段<%# Container.DataItemIndex + 1%> //实现自动编号<%# ...

  4. [原创]java向word模板中填充数据(总结)

    使用过PageOffice动态生成word文档的人都知道,PageOffice可以给word文档的指定位置进行填充,这里我们所说的指定位置在PageOffice的专业术语里面有两个概念,一个叫做数据区 ...

  5. Win7系统中用anaconda配置tensorflow运行环境

    前言:anaconda是一个python Data Science Platform.安装它的契机是因为要用tensorflow. 安装完后感觉用它来管理python运行环境还是挺方便的,常用的con ...

  6. php设置编码格式的方法

    a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312"),静态页面添加<meta htt ...

  7. WebP探索

    不同场景下WebP的使用方案总结:   App:Android4.0以上提供原生支持,其他版本使用官方提供的解析库(Android.iOS):   浏览器:JS能力检测,对支持 WebP 的用户输出 ...

  8. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  9. docker镜像管理基础

    [root@node01 ~]# docker pull quay.io/coreos/flannel:v0.10.0-amd64 v0.10.0-amd64: Pulling from coreos ...

  10. 抽屉header

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...