From d24e412980cd162e76a829b1de3112ca34a61a96 Mon Sep 17 00:00:00 2001 From: floraachy Date: Tue, 21 Apr 2020 16:25:45 +0800 Subject: [PATCH] =?UTF-8?q?python=E9=9D=A2=E8=AF=95=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unitTest/class04162020/README.txt | 4 ++ .../unitTest/class04162020/closureFunction.py | 24 +++++++++ .../unitTest/class04162020/decorator.py | 37 ++++++++++++++ .../unitTest/class04162020/intertor.py | 49 +++++++++++++++++++ .../unitTest/class04162020/jsonpython.py | 42 ++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 python27Class/unitTest/class04162020/README.txt create mode 100644 python27Class/unitTest/class04162020/closureFunction.py create mode 100644 python27Class/unitTest/class04162020/decorator.py create mode 100644 python27Class/unitTest/class04162020/intertor.py create mode 100644 python27Class/unitTest/class04162020/jsonpython.py diff --git a/python27Class/unitTest/class04162020/README.txt b/python27Class/unitTest/class04162020/README.txt new file mode 100644 index 0000000..cf30b72 --- /dev/null +++ b/python27Class/unitTest/class04162020/README.txt @@ -0,0 +1,4 @@ +课程内容: + 简历辅导 + 面试扩展知识点 + python数据与json数据之间的转换 \ No newline at end of file diff --git a/python27Class/unitTest/class04162020/closureFunction.py b/python27Class/unitTest/class04162020/closureFunction.py new file mode 100644 index 0000000..e73eebf --- /dev/null +++ b/python27Class/unitTest/class04162020/closureFunction.py @@ -0,0 +1,24 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/21 10:25 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" +""" +闭包函数:是一种特殊的函数,简称闭包。 +闭包函数的三个特性: + 函数中嵌套函数 + 外层函数返回内存嵌套的函数名 + 嵌套函数对外部作用域有一个非全局变量的引用 + +""" + +def func(): + a = 100 # func()的局部变量 + def func2(): + print(a) + return func2 + + + diff --git a/python27Class/unitTest/class04162020/decorator.py b/python27Class/unitTest/class04162020/decorator.py new file mode 100644 index 0000000..73496d3 --- /dev/null +++ b/python27Class/unitTest/class04162020/decorator.py @@ -0,0 +1,37 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/21 10:29 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" +""" +装饰器 + 语法:@装饰器函数 + 作用:在不更改原功能函数代码和调用方式的基础上给函数扩展新的功能 + 开放封闭原则: + 开放:对功能的扩展时开放的。 + 封闭:对已经实现的功能的修改是封闭的。 +""" +# --------通过闭包实现的简单装饰器--------- +# 装饰器函数必须要定义一个参数 +def func(fu): + """ + 装饰器函数 + :param fu: 被装饰的函数 + :return: + """ + def wrapper(): + print("装饰器扩展的新功能~~~~~~1") + # 调用原功能函数 + fu() + print("装饰器扩展的新功能~~~~~~2") + return wrapper + +@func # ----->print_info = func(print_info) +def print_info(): + print("原功能函数打印的个人信息") + +print_info() + + diff --git a/python27Class/unitTest/class04162020/intertor.py b/python27Class/unitTest/class04162020/intertor.py new file mode 100644 index 0000000..7577140 --- /dev/null +++ b/python27Class/unitTest/class04162020/intertor.py @@ -0,0 +1,49 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/21 16:01 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" +""" +可迭代对象:python中可以使用for去进行遍历的都叫可迭代对象 + - 字符串,列表,元组,字典, range +迭代器:可迭代对象的一种。可以通过内置函数next方法取值 +生成器:特殊的迭代器。通过yield可以定义生成器函数。 +""" + + +s = "123" +res = "-".join(["11", "22"]) +print(res) # 输出结果:11-22 + +li = [11, 22] +# 使用内置函数iter将迭代对象转换为迭代器 +ite = iter(li) +print(ite) # 输出结果: + +# 使用next每次可以迭代出一个数据 +res = next(ite) +print(res) # 输出结果:11 +res = next(ite) +print(res) # 输出结果:22 +res = next(ite) # 迭代完成后,会报错:StopIteration + +def generate(): + """ + 生成器函数 (调用的时候不会执行,会返回一个生成器对象) + :return: + """ + for i in range(100): + yield i + print("函数调用打印", i) + +generate() # 不会有任何执行结果 + +g = generate() +print(g) # 输出结果: + +print(next(g)) +print(next(g)) +print(next(g)) + diff --git a/python27Class/unitTest/class04162020/jsonpython.py b/python27Class/unitTest/class04162020/jsonpython.py new file mode 100644 index 0000000..d1d7f72 --- /dev/null +++ b/python27Class/unitTest/class04162020/jsonpython.py @@ -0,0 +1,42 @@ +""" +================================= +Author: Flora Chen +Time: 2020/4/21 10:05 +-_- -_- -_- -_- -_- -_- -_- -_- +================================= +""" +import json + +""" + Json Python +[] 数组 列表list +{} 对象 字典dict + null None + true True + false False + +""" +# --------------python转json------------------- +li = [11, "aaa", None, True, False] +# 将python的列表转换成json数组 +res1 = json.dumps(li) +print(res1, type(res1)) # 输出结果:[11, "aaa", null, true, false] + +dic = {"a": 1, "b": None, "c": True, "d": False} +# 将python的字典转换成json对象 +res2 = json.dumps(dic) +print(res2, type(res2)) # 输出结果:{"a": 1, "b": null, "c": true, "d": false} + +# --------------json转python------------------- +js1 = '[11, "aaa", null, true, false]' +# 将json的数组转换成python的列表 +res3 = json.loads(js1) +print(res3, type(res3)) # 输出结果:[11, 'aaa', None, True, False] + +js2 = '{"a": 1, "b": null, "c": true, "d": false}' +# 将json的对象转换成python的字典 +res4 = json.loads(js2) +print(res4, type(res4)) # 输出结果:{'a': 1, 'b': None, 'c': True, 'd': False} + + +