IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

装饰器、生成器,迭代器、Json & pickle 数据序列化

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、 列表生成器:代码例子

 a=[i*2 for i in range(10)]
 print(a)

 运行效果如下:
 D:\python35\python.exe D:/python培训/s14/day4/列表生成式.py
 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

 Process finished with exit code 0

2、高阶函数

变量可以指向函数,函数的参数能接受变量,即把一个函数名当做实参传给另外一个函数

返回值中包涵函数名

代码例子:

 1 def test():
 2     print("int the test")
 3 
 4 def test2(func):
 5     print("in the test2")
 6     print(func)
 7     func()
 8 
 9 test2(test)
10 运行效果如下:
11 D:\python35\python.exe D:/python培训/s14/day4/高阶函数.py
12 in the test2
13 <function test at 0x000000000110E268>  #这里是test的内存地址
14 int the test
15 
16 Process finished with exit code 0

3、装饰器

装饰器:本质是函数,(装饰其他函数)就是为其他函数添加附加功能

装饰器原则:

a.不能修改被装饰的函数的源代码

b.不能修改被装饰的函数的调用方式

实现装饰器的知识储备:

a、函数即“变量”

b、高阶函数

c、嵌套函数

高阶函数+嵌套函数=====装饰器

70_1.png

高阶函数:

a.把一个函数名当做实参传给另外一个函数

b.返回值中包含函数名

代码例子

 import  time
 def timeer(func):
     def warpper():
         start_time=time.time()
         func()
         stop_time=time.time()
         print("the fun runn time is %s" %(stop_time-start_time))
     return warpper
 @timeer
 def test1():
     time.sleep(3)
     print("in the test1")

 test1()
 运行结果如下:
 D:\python35\python.exe D:/python培训/s14/day4/装饰器.py
 in the test1
 the fun runn time is 3.000171661376953

 Process finished with exit code 0

带参数的装饰器

 import time

 def timer(func):
     def deco(*args,**kwargs):
         start_time=time.time()
         func(*args,**kwargs)
         stop_time=time.time()
         print("the func runn time is %s" %(stop_time-start_time))
     return deco

 @timer  #test1 = timer(test1)
 def test1():
     time.sleep(3)
     print("in the test1")

 @timer

 def test2(name,age):
     print("name:%s,age:%s" %(name,age))

 test1()
 test2("zhaofan",23)
 运行结果如下:

 D:\python35\python.exe D:/python培训/s14/day4/装饰器3.py
 in the test1
 the func runn time is 3.000171661376953
 name:zhaofan,age:23
 the func runn time is 0.0

 Process finished with exit code 0

终极版的装饰器

 import time
 user,passwd = "zhaofan","123"
 def auth(auth_type):
     print("auth func:",auth_type)
     def outer_wrapper(func):
         def wrapper(*args,**kwargs):
             if auth_type=="local":
                 username = input("Username:").strip()
                 password = input("Password:").strip()
                 if user == username and passwd== password:
                     print("\033[32;1mUser has passed authentication\033[0m")
                     res = func(*args,**kwargs)
                     print("------after authentication")
                     return res
                 else:
                     exit("\033[31;1mInvalid username or password\033[0m")
             elif auth_type=="ldap":
                 print("没有ldap")
         return wrapper
     return outer_wrapper

 def index():
     print("welcome to index page")
 @auth(auth_type="local")
 def home():
     print("welcome to home page")
     return "from home"
 @auth(auth_type="ldap")
 def bbs():
     print("welcome to bbs page")

 index()
 print(home())
 bbs()


 运行结果如下:
 D:\python35\python.exe D:/python培训/s14/day4/装饰器4.py
 auth func: local
 auth func: ldap
 welcome to index page
 Username:zhaofan
 Password:123
 User has passed authentication
 welcome to home page
 ------after authentication
 from home
 没有ldap

 Process finished with exit code 0

4、通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。

所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。

 a = [x for x in range(10)]
 print(a)

 g=(x for x in range(10))
 print(g)
 运行结果如下:
 D:\python35\python.exe D:/python培训/s14/day4/生成器.py
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 <generator object <genexpr> at 0x0000000000B01DB0>

 Process finished with exit code 0

生成器只有一个方法:__next__()

generator保存的是算法,每次调用next(g),就计算出g的下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出StopIteration的错误。

 g=(x for x in range(10))

 for i in g:
     print(i)


 运行结果如下:

 D:\python35\python.exe D:/python培训/s14/day4/生成器的调用.py
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9

 Process finished with exit code 0

5、可以直接作用于for循环的数据类型有以下几种

一类是集合数据类型,如listtupledictsetstr等;

一类是generator,包括生成器和带yield的generator function。

这些可以直接作用于for循环的对象统称为可迭代对象:Iterable

可以使用isinstance()判断一个对象是否是Iterable对象

可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator

生成器都是Iterator对象,但listdictstr虽然是Iterable,却不是Iterator

凡是可作用于for循环的对象都是Iterable类型;

凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;

集合数据类型如listdictstr等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。

6、json和pickle

文章永久链接:https://tech.souyunku.com/?p=30883


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(88) 打赏



未经允许不得转载:搜云库技术团队 » 装饰器、生成器,迭代器、Json & pickle 数据序列化

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367