python面试扩展

This commit is contained in:
floraachy 2020-04-21 16:25:45 +08:00
parent 52aef26038
commit d24e412980
5 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,4 @@
课程内容:
简历辅导
面试扩展知识点
python数据与json数据之间的转换

View File

@ -0,0 +1,24 @@
"""
=================================
Author: Flora Chen
Time: 2020/4/21 10:25
-_- -_- -_- -_- -_- -_- -_- -_-
=================================
"""
"""
闭包函数是一种特殊的函数简称闭包
闭包函数的三个特性
函数中嵌套函数
外层函数返回内存嵌套的函数名
嵌套函数对外部作用域有一个非全局变量的引用
"""
def func():
a = 100 # func()的局部变量
def func2():
print(a)
return func2

View File

@ -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()

View File

@ -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) # 输出结果:<list_iterator object at 0x00000239BFBA5550>
# 使用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) # 输出结果:<generator object generate at 0x000001846C654900>
print(next(g))
print(next(g))
print(next(g))

View File

@ -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] <class 'str'>
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} <class 'str'>
# --------------json转python-------------------
js1 = '[11, "aaa", null, true, false]'
# 将json的数组转换成python的列表
res3 = json.loads(js1)
print(res3, type(res3)) # 输出结果:[11, 'aaa', None, True, False] <class 'list'>
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} <class 'dict'>