可使用内置函数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也指定了默认值,最后一个调用就不会引发异常。
 
 
 

最新文章

  1. gulp 配置前端项目打包
  2. struts(二) ---中参数传值
  3. Hadoop学习之SecondaryNameNode
  4. java 获取随机数的三种方法
  5. poj1830
  6. 获取打开文件的PID
  7. 遍历Arraylist的方法
  8. 芝麻HTTP:Python爬虫实战之抓取爱问知识人问题并保存至数据库
  9. 2016-2017 CT S03E06: Codeforces Trainings Season 3 Episode 6 The Baguette Master
  10. NavigationView使用过程的问题解决
  11. 6种基础排序算法java源码+图文解析[面试宝典]
  12. mac系统 安装pip,用python读写excel(xlrd、xlwt)安装
  13. Sql Server数据字典
  14. maven 在clean package时,出现:找不到符号 [ERROR] 符号: 方法 sqlDdlFilter(java.lang.String) 解决办法
  15. python&#160;标准类库-并行执行之subprocess-子进程管理
  16. Gym 101873I - Uberwatch - [DP]
  17. swaggerui集成oauth implicit
  18. (网页)js最新手机号码、电话号码正则表达式
  19. Linux追加文件内容并在内容前加上该文件名(awk, FILENAME功能妙用)
  20. koa学习

热门文章

  1. java语言中Object对象的hashCode()取值的底层算法是怎样实现的
  2. Spring Boot2.0之性能优化
  3. mysql八:ORM框架SQLAlchemy
  4. Dual Path Networks(DPN)——一种结合了ResNet和DenseNet优势的新型卷积网络结构。深度残差网络通过残差旁支通路再利用特征,但残差通道不善于探索新特征。密集连接网络通过密集连接通路探索新特征,但有高冗余度。
  5. 【Shell】通配符与特殊符号
  6. CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)
  7. 「LOJ#10068」「一本通 3.1 练习 3」秘密的牛奶运输(次小生成树
  8. 《c# 实现p2p文件分享与传输系统》 一、 模型
  9. [转]JavaScript的实例化与继承:请停止使用new关键字
  10. C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )