Byte of Python学习笔记三
来源:互联网 发布:剑网3小和尚捏脸数据 编辑:程序博客网 时间:2024/05/22 23:46
七. 函数
函数定义
def 函数名称(参数):
函数体
参数可以没有,可能有1个或多个参数
变量三种作用域:
1. 局部变量Local variables
局部变量在函数范围内有效,函数外不可见
2. 全局变量
函数体内可以直接引用全局变量,要给全局变量赋值需要使用global进行声明。如
x = 50
def func1():
print('x is', x);
func1()
def func2():
x = 60
print('x is', x);
func2()
print('x is', x)
def func3():
global x
x = 70
print('x is', x)
func3()
print('x is', x)
输出结果:
x is 50
x is 60
x is 50
x is 70
x is 70
3. 非局部变量nonlocal variables
作用域介于局部变量和全局变量之间,在内部函数(即函数中定义函数)体中定义,由nonlocal关键字声明。如:
x = 1
def func_outer():
x = 2
def func_inner():
nonlocal x
x = 5
def func_inner_inner():
nonlocal x
x = 6
func_inner_inner()
print('x is', x)
func_inner()
print('x is', x)
func_outer()
print('x is', x)
输出结果:
x is 6
x is 6
x is 1
默认参数值
Python函数参数设置默认值,如果调用函数时没有给对应参数传值,则该参数采用默认值,默认值必须为常量且参数必须位于参数列表的最后。如
def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 3)
输出结果:
Hello
WorldWorldWorld
关键字参数
Python可以传递参数时可以通过名字将值传给指定参数。这样可以以任意顺序传入参数,还可以跳过前面的参数给后面的参数传值。如
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3,7)
func(25, c=24)
func(c=50, a=100)
输出结果:
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
可变参数
通过*来标识可变位置参数,如*param将所有的位置参数收集放到名为param的列表中,供函数中使用
通过**来标识可变关键字参数,如**param将所有关键字参数收集到名为param的字典中,供函数使用
示例:
def total(initial=5, *numbers, **keywords):
count = initial
for number in numbers:
count += number
for key in keywords:
count += keywords[key]
return count
print(total(10, 1, 2, 3, vegetables=50, fruits=100))
输出结果:166
仅关键字参数
可以通过将参数放到可变位置参数(即前面有一个星号的参数)后面的方式限制参数keyparam必须通过关键字参数的方式传参,如
def total(initial=5, *numbers, vegetables, fruits):
count = initial
for number in numbers:
count += number
count += vegetables
return count
print(total(10, 1, 2, 3, vegetables=50, fruits=60))
输出结果:66
说明:如果不需要可变位置参数,则可变位置参数可以只写一个星号,如total(initial=5, *, vegetables, fruits)
函数返回值
如果没有写return语句,则函数返回None,None是Python的一个特殊类型,类似Java语言的void. 如
def doNothing():
pass
doNothing()
输出结果:None
DocStrings
类似Java的文档注释,放在函数(或类和模块)的第一行。DocStrings一般为多行字符串,按照惯例以大写字母开头,英文句号结尾,第二行为空行,第三行开始为函数(或类和模块)详细说明。
可以通过函数名.__doc__获取函数的DocStrings,help获取的帮助信息即来自函数的DocStrings
函数定义
def 函数名称(参数):
函数体
参数可以没有,可能有1个或多个参数
变量三种作用域:
1. 局部变量Local variables
局部变量在函数范围内有效,函数外不可见
2. 全局变量
函数体内可以直接引用全局变量,要给全局变量赋值需要使用global进行声明。如
x = 50
def func1():
print('x is', x);
func1()
def func2():
x = 60
print('x is', x);
func2()
print('x is', x)
def func3():
global x
x = 70
print('x is', x)
func3()
print('x is', x)
输出结果:
x is 50
x is 60
x is 50
x is 70
x is 70
3. 非局部变量nonlocal variables
作用域介于局部变量和全局变量之间,在内部函数(即函数中定义函数)体中定义,由nonlocal关键字声明。如:
x = 1
def func_outer():
x = 2
def func_inner():
nonlocal x
x = 5
def func_inner_inner():
nonlocal x
x = 6
func_inner_inner()
print('x is', x)
func_inner()
print('x is', x)
func_outer()
print('x is', x)
输出结果:
x is 6
x is 6
x is 1
默认参数值
Python函数参数设置默认值,如果调用函数时没有给对应参数传值,则该参数采用默认值,默认值必须为常量且参数必须位于参数列表的最后。如
def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 3)
输出结果:
Hello
WorldWorldWorld
关键字参数
Python可以传递参数时可以通过名字将值传给指定参数。这样可以以任意顺序传入参数,还可以跳过前面的参数给后面的参数传值。如
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3,7)
func(25, c=24)
func(c=50, a=100)
输出结果:
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
可变参数
通过*来标识可变位置参数,如*param将所有的位置参数收集放到名为param的列表中,供函数中使用
通过**来标识可变关键字参数,如**param将所有关键字参数收集到名为param的字典中,供函数使用
示例:
def total(initial=5, *numbers, **keywords):
count = initial
for number in numbers:
count += number
for key in keywords:
count += keywords[key]
return count
print(total(10, 1, 2, 3, vegetables=50, fruits=100))
输出结果:166
仅关键字参数
可以通过将参数放到可变位置参数(即前面有一个星号的参数)后面的方式限制参数keyparam必须通过关键字参数的方式传参,如
def total(initial=5, *numbers, vegetables, fruits):
count = initial
for number in numbers:
count += number
count += vegetables
return count
print(total(10, 1, 2, 3, vegetables=50, fruits=60))
输出结果:66
说明:如果不需要可变位置参数,则可变位置参数可以只写一个星号,如total(initial=5, *, vegetables, fruits)
函数返回值
如果没有写return语句,则函数返回None,None是Python的一个特殊类型,类似Java语言的void. 如
def doNothing():
pass
doNothing()
输出结果:None
DocStrings
类似Java的文档注释,放在函数(或类和模块)的第一行。DocStrings一般为多行字符串,按照惯例以大写字母开头,英文句号结尾,第二行为空行,第三行开始为函数(或类和模块)详细说明。
可以通过函数名.__doc__获取函数的DocStrings,help获取的帮助信息即来自函数的DocStrings
- Byte of Python学习笔记三
- Byte of Python学习笔记一
- Byte of Python学习笔记二
- Byte of Python学习笔记四
- Byte of Python学习笔记五
- Byte of Python学习笔记六
- Byte of Python学习笔记七
- Byte of Python学习笔记八
- Python 学习笔记: A byte of python (一) 基础
- 《A Byte of Python》笔记
- A byte of python笔记
- a byte of python初学笔记 001
- a byte of python初学笔记 002
- 《a byte of python》学习笔记:windows下实现备份的python脚本
- vim学习笔记:byte of vim读书笔记
- A Byte of Python
- a byte of python
- Byte of Python
- jquery+笔记
- Centos6.0 64位版本上安装oracle11g
- 查询修改表
- 面向ICPC的书籍简介
- jQuery 1.6版发布,区分DOM的attributes和properties
- Byte of Python学习笔记三
- 踏入C++中的雷区——C++内存管理详解
- 《那些年啊,那些事——一个程序员的奋斗史》——38
- 《java与模式》读书笔记之四----什么是模式
- [数据库连接字符串] Access 连接字符串(转)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- 闲了
- 自定义view
- Mixed-Language Programming : How To Manage Objects(2)