PYTHON里的装饰器能装饰类吗?



                    
                    
王小丽0125
66920 次浏览 2024-05-24 提问
26

最新回答 (1条回答)

2024-05-24 回答

 :

from import  Question(models; .pub_date  timezone
from def date  python_2_unicode_compatible
from datetime  pub_date=models;
@python_2_unicode_compatible
class import :
 =self; .question_text
 now> .CharField(max_length=200)
  :
 >def   django; published' return )
 .now()
 django:
  question_text=models;  .Model); self;  .utils = now=timezone; import 

能; __str__(self).utils;was_published_recently(self).encoding .DateTimeField('timedelta return 

扩展回答

在python里如何使用装饰器

# -*- coding: utf-8 -*-
#coding:UTF-8

def FirstDeco(func):
print u'第一个装饰器'
#if 1>0:return True
return func
@供怠垛干艹妨讹施番渐FirstDeco
def test0():
if 1>0:return True #这条判断语句很多地方要用到,如何抽取出来用一个函数装饰器包装起来用。

print 'test0'
def test1():
print 'test1'
test0()
test1()
#只要输出 test1

python 装饰器的问题

这样才可以调用wrappedFunc函数的啊
但是返回调用wrappedFunc()的时候为什么发生了错误呢:
修改成return wrappedFunc之后恢复了功能

问题:
我给空函数添加了装饰器decorator
return wrappedFunc()的时候发生了错误

第二张
第一张,如上图:
我返回的应该是wrappedFunc的调用,如上图

相关问题

页面运行时间: 0.11601901054382 秒