小言_互联网的博客

Python:函数进阶

347人阅读  评论(0)

目录

一、Python中的推导式

需求一

需求二

二、Python的全局作用域

三、Python的多参数传递

四、Python的装饰器

被装饰的方法不带参数

 被装饰的方法带参数

 带参数的装饰器

一、Python中的推导式

列表生成式是python内置的一种创建列表的方法,通过在[ ]内部执行一行for循环语句,将for循环所遍

历到的元素添加到列表中。由于编译后的字节码更少, 因此比普通的采用append方法生成列表要快很

多,不仅如此,使用列表生成式编写的代码更加简洁,通过添加if else 语句,列表生成式也能对列表里

的元素进行限制。

需求一

生成一个列表,列表里有10个元素,索引为奇数的元素值为1,索引为偶数的位置值为0


  
  1. # 推导式
  2. # for i in range(10):
  3. # print(i)
  4. # 创建的列表,其中奇数位为1 偶数位为0
  5. # [1,0,1,0]
  6. #
  7. a = [i for i in range( 10)]
  8. a2 = [ 1 if i % 2 == 0 else 0 for i in range( 10)]
  9. print(a)
  10. print(a2)

需求二

取出以下课程分数大于94的科目以及具体分数


  
  1. # 推导式
  2. # 需求:取出以下课程分数大于94的科目以及具体分数
  3. class_dict = {
  4. 'c++': 90,
  5. 'python': 93,
  6. 'java': 95,
  7. 'javascript': 96,
  8. 'node.js': 94
  9. }
  10. b = [item[ 0] for item in class_dict.items()]
  11. b1 = [item[ 1] for item in class_dict.items()]
  12. print(b)
  13. print(b1)
  14. print( "====================分割线=========================")
  15. c = {item[ 0]:item[ 1] for item in class_dict.items() if item[ 1] > 94}
  16. print(c)
  17. print( "====================推导式分割线=========================")
  18. dict_new = {k: v for k, v in class_dict.items() if v > 94}
  19. print(dict_new)

二、Python的全局作用域


  
  1. # 全局作用域
  2. val = 1
  3. # val = 1 控制台运行结果
  4. def add():
  5. val = 2
  6. # val = 2 控制台运行结果
  7. # def add():
  8. # global val
  9. # val = 2
  10. add()
  11. print(val)

 

三、Python的多参数传递

参数的解体:使 *args 和 **kwargs 来调⽤函数


  
  1. # 多参数传递
  2. # 参数的解体
  3. def res( arg1, arg2, arg3):
  4. print( "arg1:", arg1)
  5. print( "arg2:", arg2)
  6. print( "arg3:", arg3)
  7. args = ( "two", 3, 5)
  8. res(*args)
  9. print( "=============")
  10. kwargs = { "arg3": 3, "arg2": "two", "arg1": 5}
  11. res(**kwargs)
  12. # 如果你想在函数⾥同时使⽤所有这三种参数, 顺序是这样的:
  13. print( "=============")
  14. args = ( "two", )
  15. kwargs = { "arg3": 3}
  16. res( "hello", *args, **kwargs)

四、Python的装饰器

闭包函数:声明在一个函数中的函数,叫做闭包函数。

闭包:内部函数总是可以访问其所在的外部函数中声明的参数和变量,即使在其外部函数被返回了之


  
  1. def outer( a):
  2. def inner( b):
  3. return a + b
  4. return inner
  5. a = outer( 1)
  6. # <function outer.<locals>.inner at 0x006DF898>
  7. print(a)
  8. # 3
  9. print(a( 2))
  10. # 5
  11. print(outer( 3)( 2))

装饰器是闭包的一种应用。 类似于java中的AOP

装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使

用装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。

被装饰的方法不带参数


  
  1. # **被装饰的方法不带参数**
  2. def transaction( func):
  3. def wrapper():
  4. print( "开启")
  5. func()
  6. print( "关闭")
  7. return wrapper
  8. @transaction
  9. def hello():
  10. print( "hello world")
  11. hello()

 被装饰的方法带参数


  
  1. # **被装饰的方法带参数**
  2. def transaction( func):
  3. def wrapper( n):
  4. print( "开启")
  5. func(n)
  6. print( "关闭")
  7. return wrapper
  8. @transaction
  9. def hello( name):
  10. print( f"{name} say hello world")
  11. hello( "zhangsan")

 带参数的装饰器


  
  1. def logging( level):
  2. def outer_wrapper( func):
  3. def inner_wrapper( *args, **kwargs):
  4. print( f"{level}: enter {func.__name__}()")
  5. return func(*args, **kwargs)
  6. return inner_wrapper
  7. return outer_wrapper
  8. @logging("error")
  9. def hello():
  10. for i in range( 3):
  11. print( 'time.sleep(1)')
  12. hello()


转载:https://blog.csdn.net/weixin_62735525/article/details/128209305
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场