- 函数式编程介绍
- 函数编程
- 局部变量
- 递归
- 高阶函数
- 嵌套函数
1、函数式编程介绍
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Weiyc
# 函数式编程跟函数不一样
# 函数式编程语言:lisp,hashshell,erlang;python支持部分,不建议从python学
2、函数编程
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Weiyc
# 面向对象:类
# 面向过程:def,没有返回值return
# 函数式编程:def,有返回值return
# import time
#
# time_format = '%Y-%m-%d %X' # 年月日小时分钟秒
# time_current = time.strftime(time_format) # 调用时间
# 使用函数的作用
# 代码重复利用
# 代码统一
# 代码可扩展
# def test1():
# print("test1")
#
# def test2():
# print("test2")
# return 0
#
# def test3():
# print("test3")
# return 0,"nihao",[1,"nihao"],{"name":"weiyc"}
#
# test1() # 调用函数,没有返回值
#
# x = test1()
# print(x) # 调用函数,有返回值
# 总结:
# 返回值为0,没有return,返回None
# 返回值为1,return一个值,返回该值
# 返回值>1,return多个值,返回tuple
# 同样可以返回一个函数
# 函数返回值作用,函数指行过程中遇到return函数停止执行并返回结果;还有就是后面程序需要根据返回值来进行调用
# def test4(x,y): # x,y形参,位置参数
# print(x)
# print(y)
#
# test4(1,2) # 1,2实参,跟形参顺序一一对应,不能多或少,位置参数
# test4(y=1,x=2) # 与形参顺序无关
# test4(3,x=3) # 按照位置参数一一对应保错,换成y=3不报错按照位置参数走,关键参数不能写在位置参数前面
# def test5(x,y=2): # y=2 默认参数
# print(x)
# print(y)
#
# test5(1)
# 默认参数特点:调用函数的时候,默认参数非必传值
# 用途:默认安装值,连接数据默认地址和端口等
# def test6(x,*args): # 参数组,应对不固定位置参数个数,默认使用args代表
# print(x)
# print(args)
#
# test6(1,2,3,4,...) # tst返回tuple
# def test7(**kwargs): # 把N个关键字参数转换为字典的方式
# print(kwargs)
# print(kwargs['name'])
# print(kwargs['age'])
# print(kwargs['job'])
#
# test7(name = 'weiyc', age = 18, job = 'it') # 调用返回字典,可无限添加
# def test8(name,hobby=1,**kwargs): # 默认参数必须在**kwargs前
# print(name)
# print(kwargs)
#
# test8('weiyc', age=18, job='it',hobby=2)
# test8('weiyc', 2,age=18, job='it')
# # name位置参数必须指定,顺序固定;默认参数顺序可以不固定或在默认参数位置写实参
#
#
#
# def test9(name,age=1,*args,**kwargs):
# print(name)
# print(args)
# print(kwargs)
#
# test9('weiyc',age=18,job='it') # *args返回空tuple,不能接受位置参数,位置参数只能给字典参数或指定位置参数
3、局部变量
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Weiyc
# age = 18 # 全局变量
# def change_name(name):
# global age # 声明,可改全局或变成一个全局变量,尽量不用,不指定哪调用就改了
# print("before",name)
# name="Weiyc" # 局部变量,所在函数是局部变量的作用域,修改不影响全局,只限于字符串、整数
# print("after",name)
# age =1
# print(age)
#
# name = "weiyc"
# change_name(name)
# print(name)
# print(age)
# 局部作用域和全局作用域的访问顺序
x = 0
def wai():
x = 1
def zhong():
x = 2
def nei():
x = 3
print(x)
nei()
zhong()
wai() # 结果为3
4、递归
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Weiyc
# 在函数内部可以调用其它函数。如果在函数内部调用自身本身,就是递归函数。
# def calc(n):
# print(n)
# return calc(n+1) # 递归层数有上限 999?,超过报错
# calc(0)
# 特性:
# 必须有一个明确的结束条件
# 每次进入更深一层递归时,问题规模应比上次递归减少
# 效率不高,过多会栈溢出
def calc(n):
print(n)
if int(n/2) > 0: # 不加int,结果为小数和负数
return calc(int(n/2))
print('>',n) # 最后一次返回,最后一次结束后执行该语句,废话
calc(10)
5、高阶函数
!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Weiyc
# 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
# 高阶函数:
# (1)把一个函数名当做实参传递给另一个函数(实现在不修改被装饰函数源代码的情况下给其添加功能)
# import time
# def bar():
# time.sleep(3)
# print('in the bar')
#
# def test1(func):
# start_time=time.time()
# func() #run bar
# stop_time=time.time()
# print("the func run time is %s" % (stop_time-start_time))
#
# test1(bar)
# (2)返回值中包含函数名(函数被装饰后,不修改其调用方式)
import time
def bar():
time.sleep(3)
print('in the bar')
def test1(func):
start_time=time.time()
func() #run bar
stop_time=time.time()
print("the func run time is %s" % (stop_time-start_time))
return func
bar=test1(bar)
bar()
6、嵌套函数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Weiyc
# 函数嵌套:在一个函数体内用def去定义一个新函数,嵌套不同于在函数体内调用其它外部已定义好的函数
def foo():
print("in the foo")
def bar(): # 在外部不可直接调用,等同于局部变量
print("in the bar")
bar()
foo()