函数与函数式编程:
二种编程模式或是编程方法:
1. 面向对象;=>> 类 =>> 定义class
2. 面向过程;=>> 过程 =》def
3. 函数式编程;最早式编程方法;=>> 函数 =>> def
编程语言中函数的定义:函数是逻辑结构化和国产化的一种编程方法;
def test(x):
"The function definitions"
x += 1
return x
def: #定义函数的关键字
test: #函数名:
(): #内可以定义参数
"": #文档描述
x += 1: #泛指代码块或程序处理过程
return: #定义返回值
# 定义函数
def func1():
"""testing"""
print("in the func1")
return 0
# 定义过程
def func2():
"""testing"""
print("in the func2")
x = func1()
y = func2()
print(x)
print(y)
输出:
in the func1
in the func2
0
None #python中隐式的给过程定义了返回值。
过程:就是没有返回值的函数;
在函数中没有定义返回值的时候,会返回一个None!
函数的优点:
1. 代码的重复利用,减少重复代码:
2. 保持一致性;
3. 可扩展性
def test():
print("in testing ")
return 0 #执行了return就代表函数结束了,后面的代码将不被执行
print("end in testing")
test()
输出:
in testing
def test():
print("in testing ")
return 0,"ss",(1,2),[1,2,3] #将返回值打包成一个元组。
a=test()
print(a)
输出:
in testing
(0, 'ss', (1, 2), [1, 2, 3])
def test():
print("in testing ")
return 0,"ss",(1,2),[1,2,3]
def test1():
print("in testing 1")
return test
print(test1)
输出:
<function test1 at 0x0350BA08> #此时返回的式test的内存地址,这个对装饰器很有作用,就是所谓的高阶函数;
带参数的函数:
def test(x,y): #x, y 是形参
print(x)
print(y)
test(1,2) #1,2 是实参,实际存在;
A. 位置参数:
实参和实参是一一对应的,位置也是一一对应;
def test(x,y):
print(x)
print(y)
test(1,2) #按位置对应
二种编程模式或是编程方法:
1. 面向对象;=>> 类 =>> 定义class
2. 面向过程;=>> 过程 =》def
3. 函数式编程;最早式编程方法;=>> 函数 =>> def
编程语言中函数的定义:函数是逻辑结构化和国产化的一种编程方法;
def test(x):
"The function definitions"
x += 1
return x
def: #定义函数的关键字
test: #函数名:
(): #内可以定义参数
"": #文档描述
x += 1: #泛指代码块或程序处理过程
return: #定义返回值
# 定义函数
def func1():
"""testing"""
print("in the func1")
return 0
# 定义过程
def func2():
"""testing"""
print("in the func2")
x = func1()
y = func2()
print(x)
print(y)
输出:
in the func1
in the func2
0
None #python中隐式的给过程定义了返回值。
过程:就是没有返回值的函数;
在函数中没有定义返回值的时候,会返回一个None!
函数的优点:
1. 代码的重复利用,减少重复代码:
2. 保持一致性;
3. 可扩展性
def test():
print("in testing ")
return 0 #执行了return就代表函数结束了,后面的代码将不被执行
print("end in testing")
test()
输出:
in testing
def test():
print("in testing ")
return 0,"ss",(1,2),[1,2,3] #将返回值打包成一个元组。
a=test()
print(a)
输出:
in testing
(0, 'ss', (1, 2), [1, 2, 3])
def test():
print("in testing ")
return 0,"ss",(1,2),[1,2,3]
def test1():
print("in testing 1")
return test
print(test1)
输出:
<function test1 at 0x0350BA08> #此时返回的式test的内存地址,这个对装饰器很有作用,就是所谓的高阶函数;
带参数的函数:
def test(x,y): #x, y 是形参
print(x)
print(y)
test(1,2) #1,2 是实参,实际存在;
A. 位置参数:
实参和实参是一一对应的,位置也是一一对应;
def test(x,y):
print(x)
print(y)
test(1,2) #按位置对应
B. 关键字参数
def test(x,y):
print(x)
print(y)
test(y=1,x=2) #关键字对应。与形参位置没关系;
如果同时存在关键参数和位置参数,关键参数是不能放在位置参数前面
也就是说从左往右,位置参数前面不能有关键参数。
如果存在关键字参数就不论位置了。
C. 默认参数:
def test(x,y=1): #即在形参中对对参设置默认值
print(x)
print(y)
test(1,y=2)
默认参数的特点,调用函数时,默认参数非必须传值
用途:默认安装值,
D. 参数组:
针对实参不固定的情况,如何定义形参,即参数组的概念:
def test(*args): # *代表一个功能代号,表示实参不固定, args只是一个名字,可以随便,但规范是args:
print(args)
test(1,2,3,4,5)
test(*[1,2,3,4]) # *代表一个功能代号,表示实参不固定
输出:
(1, 2, 3, 4, 5)
(1, 2, 3, 4)
def test(x, *args):
print(x)
print(args)
test(1, 2, 3, 4, 5)
test(*[1,2,3,4])
输出:
1
(2, 3, 4, 5) # 返回元组
1
(2, 3, 4)
接收字典形参:
def test2(**kwargs): # **代表不固定形参为字典
print(kwargs)
test2(name='alex',age='9',gender="female")
test2(**{name='alex',age='9',gender="female"})
输出:
{'name': 'alex', 'age': '9', 'gender': 'female'} # 返回字典
将n个关键字参数转化为字典的方式。
def test2(**kwargs):
print(kwargs["name"])
print(kwargs["age"])
print(kwargs["gender"])
test2(name='alex', age='9', gender="female")
输出:
alex
9
female
def test2(x, **kwargs):
print(x)
print(kwargs)
test2(1, name='alex', age='9', gender="female")
输出:
1
{'name': 'alex', 'age': '9', 'gender': 'female'}
def test2(x, age=18, **kwargs): #参数组必须放在位置参数,关键字参数后面
print(x)
print(age) #关键字需要独立出来,作为一个独立的参数
print(kwargs)
test2(1, name='alex', age='9', gender="female")
输出:
1
9
{'name': 'alex', 'gender': 'female'}
*args 接收的是N个位置参数转换为元组的方式
**kwargs 接收的是N个关键字参数转化为字典的方式
===========================================================