IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python的装饰器 -> 正文阅读

[Python知识库]python的装饰器

python的装饰器

面向对象的理解:一切皆对象

def test(name='ksks14'):
    '''这是是注释'''
    return 'hello '+name

print(test.__name__)
print(test.__doc__)
print(test())
test
这是是注释
hello ksks14
'''
将test函数赋值给另一个值
'''
# 注意赋值给的名而不是整个函数
new_test=test

print(new_test.__name__)
print(new_test.__doc__)
print(new_test())
test
这是是注释
hello ksks14

结果说明虽然是赋值,但实际上是类似于一个指向的作用

  • 这时候删除掉test函数试试,报错

进一步走,python支持在函数中定义另外一个函数。

def test(name="test"):
    print("Enter a function:test")
    def test_1(name="test_1"):
        print("Enter a function:test_1")
    def test_2(name="test_2"):
        print("Enter a function:test_2")
        
    test_1()
    test_2()
test()
Enter a function:test
Enter a function:test_1
Enter a function:test_2

进一步走,函数中自然也可以返回函数

def test(name="test"):
    def test_1():
        return "Enter a function:test_1"
    def test_2():
        return "Enter a function:test_2"
        
    if name=="test":
        return test_1()
    else:
        return test_2()

test("test")
'Enter a function:test_1'

总结发现,对于python的函数,如果将小括号放在后面就会执行,而如果单用名字则可以进行传递。既然可以进行传递,那么就可以将函数作为参数传递。

def out_func(func):
    
    def wrap():
        print("外层在执行函数前")
        func()
        print("外层在执行函数后")
    
    # 这里返回函数名,用来传递测试
    return wrap

def test_wrap():
    print("执行函数")
    
test = out_func(test_wrap)

test()
外层在执行函数前
执行函数
外层在执行函数后
def out_func(func):
    
    def wrap():
        print("外层在执行函数前")
        func()
        print("外层在执行函数后")
    
    # 这里返回函数名,用来传递测试
    return wrap

@out_func
def test_wrap():
    print("执行函数")
    

test_wrap()
外层在执行函数前
执行函数
外层在执行函数后

可以分析得到,在利用@之后,再次执行了一次封装,所以又多了两行外函数的输出同时使用了@之后可以直接调用test_wrap()

但是这里存在问题,输出name试试

print(test_wrap.__name__)
wrap

这里想要的应该是out_func而不是wrap,显然被修改了。

from functools import wraps
def out_func(func):
    @wraps(func)
    def wrap():
        print("外层在执行函数前")
        func()
        print("外层在执行函数后")
    
    # 这里返回函数名,用来传递测试
    return wrap

@out_func
def test_wrap():
    print("执行函数")
    

test_wrap()
print(test_wrap.__name__)
外层在执行函数前
执行函数
外层在执行函数后
test_wrap

显然这获取到了函数在修饰前的name,@wraps接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。

from functools import wraps

def logit(func):
    @wraps(func)
    # 这里的参数会传入到addition_func
    def with_logging(*args, **kwargs):
        print(func.__name__ + " was called")
        return func(*args, **kwargs)
    return with_logging

@logit
def addition_func(x):
    """Do some math."""
    return x + x
 

result = addition_func(4)
print(result)
addition_func was called
8

接着我们来看web登录状态判断的操作

import datetime
from functools import wraps
import pytz
from django.contrib import messages
from django.shortcuts import render, redirect, get_object_or_404



def is_login(func):
    @wraps(func)
    # 这里需要传入一个request
    def wrapper(request,*args,**kwargs):
        # 通过session获取一个user,进行判断
        user=request.session.get("user",None)
        if user:
            return func(request,*args,**kwargs)
        else:
            messages.error(request, "用户未登录,请先登录!")
            # 回退到登录操作
            return redirect(reverse('login'))
    return wrapper


# 这里通过函数装饰器进行用户判断。
@is_login
def index(request):
    return redirect(reverse('play_list'))
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-30 12:42:21  更:2021-07-30 12:44:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 5:57:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码