PYTHON里的装饰器能装饰类吗

2024-06-24 14:48:09 (19分钟前 更新) 402 4472

最新回答

 :

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 

 :

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 

lukylukycat 2024-06-24

扩展回答

3

在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

4

python 装饰器的问题

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

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

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

热门问答

装修专题

其他人还看了

页面运行时间: 0.24162411689758 秒