装饰师基类:需要改进。 [英] Decorator Base Class: Needs improvement.

查看:58
本文介绍了装饰师基类:需要改进。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



再次感谢所有帮助我了解

装饰的细节。


我把它放在一起创建可以使它们很多的装饰器的类

更容易使用。


它仍然有一些需要解决的问题。 />

(1)''函数''对象的测试不需要测试字符串

而是测试对象类型。


(2)如果堆叠了相同的装饰器实例,它将被锁定在

循环中。但是堆叠由相同的

装饰器对象创建的不同实例工作正常。


(3)如果装饰器有多个参数,则会有问题。 />
但我认为所有这些都可以修复。这可能是内置库中的内容吗? (任何问题解决之后

当然首先。)


当它们堆叠起来时,它们会首先处理所有的前期,然后调用

装饰器,然后处理所有后期处理'。它只是用这种方式制作了

,这是一个很好的惊喜,使得这项工作与标准装饰器不同。




干杯,

罗恩


#---开始---


类装饰器(对象) :

"""

装饰器 - 用于制作装饰器的类。


self.function - 名称函数装饰。

self.arglist - 装饰器的参数

self.preprocess - 过度预处理函数参数。

self.postprocess - over乘车到后期处理功能

返回值。


使用示例:

class mydecorator(装饰):

def self.preprocess(self,args):

#process args

return args

def self.postprocess(self,results) :

#处理结果

返回结果


deco = mydecorator()


@deco

def函数(args):

#函数机构

返回args

"""

功能=无

arglist = []

def __call __(self,arg):

self.arglist.append(arg)

def _wrapper(args):

pre_args = self.preprocess(args)

result = self.function(pre_args)

返回self.postprocess(结果)

if str中的''function''(arg):

self.arglist = self.arglist [: - 1]

self.function = arg

return _wrapper

返回self

def preprocess(self,args):

return args

def postprocess (自我,结果):

返回结果


类mydecorator(Decorater):

def preprocess(self,args):

args = 2 * args

return args

def postprocess(self,args):

args = args .upper()

args = args + str(self.arglist [0])

return args

deco = mydecorator()


@deco(''xyz'')

def foo(文字):

返回文字


打印foo(''abc'')

# ---结束---


Hi, Thanks again for all the helping me understand the details of
decorators.

I put together a class to create decorators that could make them a lot
easier to use.

It still has a few glitches in it that needs to be addressed.

(1) The test for the ''function'' object needs to not test for a string
but an object type instead.

(2) If the same decorator instance is stacked, it will get locked in a
loop. But stacking different instances created from the same
decorator object works fine.

(3) It has trouble if a decorator has more than one argument.
But I think all of these things can be fixed. Would this be something
that could go in the builtins library? (After any issues are fixed
first of course.)

When these are stacked, they process all the prepossess''s first, call
the decorator, then process all the postprocess''s. It just worked out
that way, which was a nice surprise and makes this work a bit
different than the standard decorators.

Cheers,
Ron

#---start---

class Decorator(object):
"""
Decorator - A class to make decorators with.

self.function - name of function decorated.
self.arglist - arguments of decorator
self.preprocess - over ride to preprocess function arguments.
self.postprocess - over ride to postprocess function
return value.

Example use:
class mydecorator(Decorate):
def self.preprocess(self, args):
# process args
return args
def self.postprocess(self, results):
# process results
return results

deco = mydecorator()

@deco
def function(args):
# function body
return args
"""
function = None
arglist = []
def __call__(self, arg):
self.arglist.append(arg)
def _wrapper( args):
pre_args = self.preprocess(args)
result = self.function(pre_args)
return self.postprocess(result)
if ''function'' in str(arg):
self.arglist = self.arglist[:-1]
self.function = arg
return _wrapper
return self
def preprocess(self, args):
return args
def postprocess(self, result):
return result

class mydecorator(Decorater):
def preprocess(self, args):
args = 2*args
return args
def postprocess(self, args):
args = args.upper()
args = args + str(self.arglist[0])
return args
deco = mydecorator()

@deco(''xyz'')
def foo(text):
return text

print foo(''abc'')
#---end---

推荐答案



好​​的,那个帖子可能有几个(打?)的问题它。由于闲置不清除变量之间的变量,我得到了一些故障

因此它对我有用

因为它从之前的运行中得到了值。


这应该更好,修正了一些东西。


装饰者现在可以使用多个参数。

函数和参数列表现在正确初始化。


它不适用于具有多个变量的函数。似乎

元组在给作为参数的函数时不会解包。任何方式

强制它?

类装饰(对象):

"""

装饰器 - 用于制作装饰器的基类。


self.function - 装饰的函数名称。

self.arglist - 装饰器的参数

self.preprocess - over ride to preprocess function arguments。

self.postprocess - over ride to postprocess function

返回值。

使用示例:

class mydecorator(装饰):

def self.preprocess(self,args):

#process args

return args

def self.postprocess(self,results):

#process results

返回结果


deco = mydecorator()


@deco

def函数(args):

#function body

return args

"""

def __init __(self):

self.function =无

self.arglist = []

def __call __(self,* arg) :

如果len(arg)== 1:

arg = arg [0]

self.arglist.append(arg)

def _wrapper(* args):

如果len(args)== 1:

args = args [0]

pre_args = self.preprocess(args)

result = self.function(pre_args)

返回self.postprocess(结果)

if str中的''function''(arg):

self.arglist = self.arglist [: - 1]

self.function = arg

return _wrapper

返回self

def preprocess(self,args):

return args

def postprocess (自我,结果):

返回结果

#--- 3 ---

类mydecorator(装饰员):

def preprocess(self,args):

args = 2 * args

return args

def postprocess(self,args) :

args = args.upper()

args = args + str(self.arglist [0])

return args

deco = mydecorator()


@deco(''xyz'')

def foo(text):

返回文字

print foo(''abc'')

#--- 2 ---

class decorator2(Decorator):

def preprocess(self,args):

return args + sum(self.arglist [0])

def postprocess(self,args):

return args

deco2 = decorator2()


@ deco2(1,2)

def foo(a):

返回

打印foo(1)


#这个还行不通。

#--- 3 ---

class decorator3(装饰):

传递

deco3 = decorator3()


@ deco3

def foo(a,b):

返回a,b

print foo(1,3)

Ok, that post may have a few(dozen?) problems in it. I got glitched
by idles not clearing variables between runs, so it worked for me
because it was getting values from a previous run.

This should work better, fixed a few things, too.

The decorators can now take more than one argument.
The function and arguments lists initialize correctly now.

It doesn''t work with functions with more than one variable. It seems
tuples don''t unpack when given to a function as an argument. Any way
to force it?
class Decorator(object):
"""
Decorator - A base class to make decorators with.

self.function - name of function decorated.
self.arglist - arguments of decorator
self.preprocess - over ride to preprocess function arguments.
self.postprocess - over ride to postprocess function
return value.

Example use:
class mydecorator(Decorate):
def self.preprocess(self, args):
# process args
return args
def self.postprocess(self, results):
# process results
return results

deco = mydecorator()

@deco
def function(args):
# function body
return args
"""
def __init__(self):
self.function = None
self.arglist = []
def __call__(self, *arg):
if len(arg) == 1:
arg = arg[0]
self.arglist.append(arg)
def _wrapper( *args):
if len(args) == 1:
args = args[0]
pre_args = self.preprocess(args)
result = self.function(pre_args)
return self.postprocess(result)
if ''function'' in str(arg):
self.arglist = self.arglist[:-1]
self.function = arg
return _wrapper
return self
def preprocess(self, args):
return args
def postprocess(self, result):
return result
#---3---
class mydecorator(Decorator):
def preprocess(self, args):
args = 2*args
return args
def postprocess(self, args):
args = args.upper()
args = args + str(self.arglist[0])
return args
deco = mydecorator()

@deco(''xyz'')
def foo(text):
return text
print foo(''abc'')
#---2---
class decorator2(Decorator):
def preprocess(self, args):
return args+sum(self.arglist[0])
def postprocess(self, args):
return args
deco2 = decorator2()

@deco2(1,2)
def foo(a):
return a
print foo(1)

# This one doesn''t work yet.
#---3---
class decorator3(Decorator):
pass
deco3 = decorator3()

@deco3
def foo(a,b):
return a,b
print foo(1,3)


Ron_Adam写道:
Ron_Adam wrote:
好的,该帖子可能有一些(打)?问题。我在运行之间没有清除变量,因为它没有清除变量,因此它对我有用
因为它从以前的运行中获得了值。

这应该更好,修复了一些事情也是如此。

装饰者现在可以使用多个参数。
函数和参数列表现在正确初始化。

Ron:


我已经跟踪了您对感兴趣的装饰者的尝试,并且

已经看到您与Python的许多杰出人物进行对话

社区所以我在这一点上犹豫是否插入我自己的言论。


但是,本着乐于助人的精神,我不得不问你是否对装饰者有所了解是不同于我的,因为你不会理解他们或因为我不是。


你有好几次提到装饰的可能性采取

不止一个参数,但在我对装饰器的理解中这只是

没有意义。装饰者应该(不应该)精确地使用一个参数(一个函数或一个方法)并且只返回一个值(一个

装饰函数或方法)。

它不适用于具有多个变量的函数。看来,当赋予函数作为参数时,元组不会解包。什么方式强迫它?

类装饰(对象):
Ok, that post may have a few(dozen?) problems in it. I got glitched
by idles not clearing variables between runs, so it worked for me
because it was getting values from a previous run.

This should work better, fixed a few things, too.

The decorators can now take more than one argument.
The function and arguments lists initialize correctly now.
Ron:

I''ve followed your attempts to understand decorators with interest, and
have seen you engage in conversation with many luminaries of the Python
community, so I hesitate at this point to interject my own remarks.

In a spirit of helpfulness, however, I have to ask whether your
understanding of decorators is different from mine because you don''t
understand them or because I don''t.

You have several times mentioned the possibility of a decorator taking
more than one argument, but in my understanding of decorators this just
wouldn''t make sense. A decorator should (shouldn''t it) take precisely
one argument (a function or a method) and return precisely one value (a
decorated function or method).
It doesn''t work with functions with more than one variable. It seems
tuples don''t unpack when given to a function as an argument. Any way
to force it?
class Decorator(object):



[...]


也许我们需要回归基础?


当我说装饰者应该使用一个

函数作为参数时,你明白我的意思吗?它应该返回一个函数吗?


问候

Steve

-

Steve Holden + 1 703 861 4237 +1 800 494 3119

Holden Web LLC http:/ /www.holdenweb.com/

Python网页编程 http ://pydish.holdenweb.com/


[...]

Perhaps we need to get back to basics?

Do you understand what I mean when I say a decorator should take one
function as its argument and it should return a function?

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/


Steve Holden写道:
Steve Holden wrote:
你有几次提到装饰者
的可能性超过一个参数,但在我对装饰器的理解中,这个
只是没有意义。装饰者应该(不应该)精确地接受
一个参数(一个函数或一个方法),然后返回一个值
(装饰函数或方法)。
You have several times mentioned the possibility of a decorator taking more than one argument, but in my understanding of decorators this just wouldn''t make sense. A decorator should (shouldn''t it) take precisely one argument (a function or a method) and return precisely one value (a decorated function or method).




是的。我认为这可以通过这种方式固定在人们的脑海中

这样你就说明了:


写作时


@decorator(x,y)

def f():

....


不是这样称为装饰者函数但装饰器(x,y)是

装饰函数,而装饰器(x,y)只不过是一个可调用对象

,它将f作为参数。你的陈述的一点点修正:它是

不是必须从

装饰者那里返回一个函数或方法。


def decorator(x,y):

def inner(func):

返回x + y

返回内部
< br $>
@decorator(1,2)

def f():传递



Yes. I think this sould be fixed into the minds of the people exacly
this way You state it:

When writing

@decorator(x,y)
def f():
....

not the so called "decorator" function but decorator(x,y) is the
decorating function and decorator(x,y) is nothing but a callable object
that takes f as parameter. A little correcture of Your statement: it is
NOT nessacary that a function or method will be returned from a
decorator.

def decorator(x,y):
def inner(func):
return x+y
return inner

@decorator(1,2)
def f():pass

f



3


这完全有效,尽管不是很有用;)


问候,




3

This is perfectly valid allthough not very usefull ;)

Regards,
Kay


这篇关于装饰师基类:需要改进。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆