总结
项目 | 描述 | 备注 |
---|---|---|
全局变量 | 修改关键字:gloab | 作用域:全局 |
局部变量 | / | 作用域:函数内部 |
嵌套函数变量 | 修改关键字:nonlocal | 作用域:嵌套函数可访问被嵌套函数的变量 |
参数默认值 | fun(x=10) | 参数缺省值,自动使用默认值 |
lamdba | lambda val1, val2: expression | |
map | 遍历,将每个元素作为参数输入func | map(lamdba x:x*2, iterable) |
filter | 过滤,返回func true的值 | filter(lambda x:x%2==0, iterabel) |
reduce | x1,x2两两作为参数输入func | reduce(lambda x, y: x * y, iterable) |
绝对路径import | from project.targetmodule import * | |
相对路径import | 上层目录sys.path.append('..') | 上上层目录sys.path.append('../..') |
变量作用域
局部变量
# 局部变量
def local_val(arg):
print('local_val arg is {}'.format(arg))
arg = 10
print('local_val arg change is {}'.format(arg))
if __name__ == '__main__':
arg = 11
local_val(arg)
#######
#local_val arg is 11
#local_val arg change is 10
全局变量
GLOABL_VAL = 10
# 全局变量
def global_val():
global GLOABL_VAL
GLOABL_VAL = 11
print('gloabl_val is {}'.format(GLOABL_VAL))
if __name__ == '__main__':
print('gloabl_val is {}'.format(GLOABL_VAL))
global_val()
print('gloabl_val is {}'.format(GLOABL_VAL))
#######
#gloabl_val is 10
#gloabl_val is 11
#gloabl_val is 11
嵌套函数变量
# 嵌套函数访问外部函数变量
def nonlocal_val():
tempa = 11
print('nonlocal tempa is {}'.format(tempa))
def change_nonlocal_val():
nonlocal tempa
tempa = 10
print('nonlocal tempa is {}'.format(tempa))
change_nonlocal_val()
print('nonlocal tempa is {}'.format(tempa))
if __name__ == '__main__':
nonlocal_val()
############
#nonlocal tempa is 11
#nonlocal tempa is 10
#nonlocal tempa is 11
闭包
与嵌套函数类似,不同的是闭包外部函数返回的是一个函数,而不是一个具体的值
- 装饰器的工作原理
- exponent能被内部嵌套函数exponent_of所使用
- val为内部嵌套函数的参数
- return exponent_of返回函数
def nth_power(exponent):
def exponent_of(val):
return val ** exponent
return exponent_of
if __name__ == '__main__':
square = nth_power(2)
cube = nth_power(3)
print(square(3))
print(cube(3))
函数
匿名函数lambda
lamdba: val1, val2, val3: expression
- lambda是一个表达式(expression), 并不是一个语句(statement)
- lambda的主体只有一行简单的表达式,并不能扩展成一个多行的代码块
实例
temp = [(lambda x: x * x)(x) for x in range(10)]
print(temp)
函数式编程
- filter:
filter() 函数表示对 iterable 中的每个元素,都使用 function 判断,并返回 True 或者 False,最后将返回 True 的元素组成一个新的可遍历的集合 - map:
函数 map(function, iterable) 的第一个参数是函数对象,第二个参数是一个可以遍历的集合,它表示对 iterable 的每一个元素,都运用 function 这个函数。 - reduce
它通常用来对一个集合做一些累积操作。function 同样是一个函数对象,规定它有两个参数,表示对 iterable 中的每个元素以及上一次调用后的结果,运用 function 进行计算,所以最后返回的是一个单独的数值。
l = [(1, 20), (3, 0), (9, 10), (2, -1)]
l1 = sorted(l, key=lambda x: x[1])
print(l1)
squared = map(lambda x: x * x, [1, 2, 3, 4, 5])
print(list(squared))
filter_val = filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6])
print(list(filter_val))
product = reduce(lambda x, y: x * y, [1, 2, 3, 4, 5, 6])
print(product)
模块化
函数的引用问题
如下图为工作目录,test->class13

绝对路径引用
main.py调用utils->class_utils.py文件的Encoder类方法endocer
from class13.utils.class_utils import Encoder
src.sub_main调用utils->class_utils.py文件的Encoder类方法endocer
from class13.utils.class_utils import Encoder
相对路径应用
- 当前层
main.py调用utils->class_utils.py文件的Encoder类方法endocer
from utils.class_utils import Encoder
- 上层
src.sub_main调用utils->class_utils.py文件的Encoder类方法endocer
import sys
sys.path.append('..')
from class_utils import Encoder
- 上上层
src.src_sub.sub_sub_main调用utils->class_utils.py文件的Encoder类方法endocer
import sys
sys.path.append('../..')
from class_utils import Encoder
原理
实际上,Python 解释器在遇到 import 的时候,它会在一个特定的列表中寻找模块。这个特定的列表,可以用下面的方式拿到:
import sys
print(sys.path)
###输出
['/Users/jc/Desktop/技术栈/python/test/class13/src/src_sub',
'/Users/jc/Desktop/技术栈/python/test',
'/Applications/PyCharm.app/Contents/helpers/pycharm_display',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Users/jc/Library/Python/3.7/lib/python/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']
网友评论