尚未提及的装饰器语法(我想!) [英] A decorator syntax not yet mentioned (I think!)

查看:45
本文介绍了尚未提及的装饰器语法(我想!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面怎么样,我几乎是肯定的

没有被建议:

-----

class Klass:

def __init __(自我,姓名):

self.name = name


deco meth0:

staticmethod

def meth0(x):

返回x


deco meth1:

classmethod

def meth1(cls):

返回cls


deco sayhello:

funcattrs( name ='''GvR'',language =''python'')

log(file =''func.log'')

def sayhello(self):

打印''hello python world''


-----

1)装饰者明确适用于特定的方法/函数,

因此在方法/函数定义完成之前不需要在内存中推送任何堆栈



2装饰器是外部的。方法/功能他们

装饰:

a)这将取悦那些想要外面的位置

b)不会折叠在功能内

c)可以在装饰器上进行折叠,以便

def不被混淆

d)可以位于代码中的任何位置 - 但是很可能是在def ...()之前的


3)应用装饰器的顺序只是

就像代码一样 - 这肯定是直观的,因为

我们正在编写代码。


这种方法也可以应用于类,如果

装饰者应该扩展到他们:

-----

deco Klass:

doc(" This是一个班级......

班级Klass:





--- -


有什么意见吗?


John

How about the following, which I am almost positive
has not been suggested:
-----
class Klass:
def __init__(self, name):
self.name = name

deco meth0:
staticmethod
def meth0(x):
return x

deco meth1:
classmethod
def meth1(cls):
return cls

deco sayhello:
funcattrs(name=''GvR'', language=''python'')
log(file=''func.log'')
def sayhello(self):
print ''hello python world''

-----
1) The decorators clearly apply to a specific method/function,
therefore there is no need to do any stack pushing in memory
until the method/function definition is done.
2) The decorators are "outside" of the method/function they
decorate:
a) which will please those who want the outside location
b) will not be folded within the function
c) folding on the decorators can be done so that the
def is not obfuscated
d) can be located anywhere in the code--but most likely
before the "def ...()"
3) The sequence in which the decorators are applied is just
like code would be--this is certainly intuitive given
that we are writing code.

This approach could also be applied to classes in case
decorators should ever be extended to them:
-----
deco Klass:
doc("This is a class to ...")
class Klass:
:
:
-----

Any comments?

John

推荐答案

John Marshall写道:
John Marshall wrote:
下面怎么样,我几乎是积极的
没有被建议:
-----
类Klass:
def __init __(self,name):
self.name = name

deco meth0:
staticmethod
def meth0(x):
返回x

- ----
1)装饰器明确适用于特定的方法/功能,因此在完成方法/功能定义之前,不需要在内存中进行任何堆栈推送。


问题:重复(因为你现在在两个地方有名字

的功能)和可读性(它更难以

看到def,因为你选择的名字离它太近了

的长度和外观)。


另一方面,没有必要有功能名称

那里(有人抱怨堆栈推送?),所以

可能只是使用装饰"没关系,或者是另一个

关键字:


类Klass:

#init set

装饰:

staticmethod

def meth0(x):

返回x

2)装饰者是在外面。它们装饰的方法/功能:
a)会让那些想要外面位置的人b / b)不会折叠在功能内
c)折叠装饰器可以这样做,以便
def不被混淆
d)可以位于代码中的任何位置 - 但很可能是在def ...()之前
3)应用装饰器的顺序就像代码一样 - 这肯定是直观的,因为我们正在编写代码。
有什么意见吗?
How about the following, which I am almost positive
has not been suggested:
-----
class Klass:
def __init__(self, name):
self.name = name

deco meth0:
staticmethod
def meth0(x):
return x

-----
1) The decorators clearly apply to a specific method/function,
therefore there is no need to do any stack pushing in memory
until the method/function definition is done.
Problems with that: duplication (since you now have the name
of the function in two places) and readability (it''s harder to
see the "def" because the name you picked is too close to it
in length and appearance).

On the other hand, there''s no need to have the function name
there (was anyone complaining about "stack pushing"?), so
maybe just using "decorate" would be okay, or a different
keyword:

class Klass:
# init set

decorate:
staticmethod
def meth0(x):
return x
2) The decorators are "outside" of the method/function they
decorate:
a) which will please those who want the outside location
b) will not be folded within the function
c) folding on the decorators can be done so that the
def is not obfuscated
d) can be located anywhere in the code--but most likely
before the "def ...()"
3) The sequence in which the decorators are applied is just
like code would be--this is certainly intuitive given
that we are writing code. Any comments?




我认为有一些变化有潜力。


我喜欢以下关于它:


1)关键字而不是标点符号

2)缩进与Python的其余部分一致

3)相似性尝试/终于或者同时/和朋友们在哪里

两个条款相结合


我不介意看到完全相同的想法允许

decorators *里面*这个函数也是如此,这样我们就可以实验一段时间实验
看看哪个(如果有的话)是首选的,

但是我怀疑那将会发生。


-Peter



Has potential, I think, with some changes.

I like the following about it:

1) keyword instead of punctuation
2) indentation consistent with rest of Python
3) similarity to try/finally or while/else and friends where
the two clauses are coupled

I wouldn''t mind seeing exactly the same idea allowed for
decorators *inside* the function as well, so that we can
experiment for a while and see which, if any, is preferred,
but I doubt that would happen.

-Peter


Peter Hansen写道:
Peter Hansen wrote:
class Klass:
#init set

装饰:
staticmethod
def meth0(x):
返回x
class Klass:
# init set

decorate:
staticmethod
def meth0(x):
return x




如果有任何价值,请+1。


这对我来说非常不错对于

目前的建议是什么。


配对关键字的想法在python中很常见

打击我另一个正面元素。


使用 http: //www.python.org/moin/PythonDecorators


您获得类似的结果:

class C(object):


装饰:

staticmethod,

funcattrs(语法=&';'''''dotted_name [''(''[arglist]' ')'']",

status =" experimental",author =" BDFL")

def longMethodNameForEffect(longArgumentOne = None,

longArgumentTwo = 42):

"""这个方法等等,等等。


它支持以下参数:

- longArgumentOne - 给出的字符串...

- longArgumentTwo - 给出的数字......


blah,blah。


"""

加注NotYetImplemented


并且:


装饰:

接受(int,int)

返回(浮动)

def bar(低,高):

通过


这让我觉得非常好 - 事实上这个方法是一种静态的方法

仍然在你面前。 decorate关键字表示一些伏都教

正在发生你可能想要警惕。


唯一的缺点我可以看到它可能不是100%明显那个
与python的新手有关的以下功能。


另一个非常好的事情是它直接暗示了

以下函数将应用于该函数。


从一个简单的PLY类型语法的子集中获取一些东西

如:


class Calc(Parser):

装饰:

tokenrule(r''\ +'')

def t_NUMBER(self,t):

t.value = int(t.value)

返回t


装饰:

tokenrule(r''\\ n +'')

def t_newline(self,t):

t.lineno + = t.value.count(" \ n")

decorate:

grammarrule(''statement:NAME EQUALS expression'')

versioninfo("已添加在2.2)

typeinfo(int)

def p_statement_assign(self,p):

names [p [1]] = p [3]


装饰:

grammarrule(''陈述:表达'')

versioninfo(" 2.4) ")

已弃用

typeinfo(无)

def p_statement_expr(self,p):

print p [1]


装饰:

grammarrule("""

表达式:表达式PLUS表达

|表达式MINUS表达式

|表达式TIMES表达式

|表达式DIVIDE表达式

|表达式EXP表达式

""")

versioninfo(在2.4中添加)

typeinfo(int)

def p_expression_binop(self,p):

p [0] = func [p [2]](p [1],p [3])


装饰:

grammarrule(''表达式:MINUS表达式%prec UMINUS'')

versioninfo(在1.5中添加)

typeinfo(int)

def p_expression_uminus(self,p):

p [0] = -p [2]

装饰:

grammarrule(''表达式:LPAREN表达式RPAREN'')

versioninfo(在1.1中添加)

typeinfo(int)

def p_expression_group(self,p):

p [0] = p [2]


在语法高亮的编辑器中,预先看起来*比* / b $ b更清洁。 IMO,它看起来也比@version更干净 - 对于

一个非常非常简单的原因 - 如果你有一个非常大的装饰块,

无论出于何种原因, " DEF"很明显,所以你可以浏览功能开始的地方 - 即使没有语法高亮。


我对@decorators的最初反应是呃! 。然而,在阅读了各种语法/ /
各种语法的各种论据之后,我来到了

接受它是不可避免的。 *这个*语法让我个人感到震惊,因为它具有明显的优势 - 主要是为了清晰的代码,并解决了

其中* DOES *函数的开始?大装饰的问题

街区。


我的tuppenceworth ...(我真的很喜欢这个版本)

Michael。

-
Mi ******* *****@rd.bbc.co.uk

英国广播公司,研发部门

Kingswood Warren,Surrey KT20 6NP


此消息(和任何附件)可能包含个人观点

除非另有说明,否则不是BBC的观点。



If it counts for anything, +1.

This strikes me personally as a very nice alternative to the
current suggestion.

The idea of paired keywords is sufficiently common in python
to strike me as another positive element.

Using the examples on http://www.python.org/moin/PythonDecorators

You gain something like:
class C(object):

decorate:
staticmethod,
funcattrs(grammar="''@'' dotted_name [ ''('' [arglist] '')'' ]",
status="experimental", author="BDFL")
def longMethodNameForEffect(longArgumentOne=None,
longArgumentTwo=42):
"""This method blah, blah.

It supports the following arguments:
- longArgumentOne -- a string giving ...
- longArgumentTwo -- a number giving ...

blah, blah.

"""
raise NotYetImplemented

And:

decorate:
accepts(int,int)
returns(float)
def bar(low,high):
pass

Which strikes me as pretty good - the fact the method is a static method
is still in your face. The decorate keyword suggests some voodoo
happening you might want to be wary of.

The only downside I can see is that it might not be 100% obvious that it
relates to the following function to newcomers to python.

The other really nice thing about it is it directly suggests that the
following functions will be applied to the function.

Taking a subset of a simple PLY type grammar from, you gain something
like:

class Calc(Parser):
decorate:
tokenrule(r''\d+'')
def t_NUMBER(self, t):
t.value = int(t.value)
return t

decorate:
tokenrule(r''\n+'')
def t_newline(self, t):
t.lineno += t.value.count("\n")

decorate:
grammarrule(''statement : NAME EQUALS expression'')
versioninfo("Added in 2.2")
typeinfo(int)
def p_statement_assign(self, p):
names[p[1]] = p[3]

decorate:
grammarrule(''statement : expression'')
versioninfo("Added in 2.4")
deprecated
typeinfo(None)
def p_statement_expr(self, p):
print p[1]

decorate:
grammarrule("""
expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
| expression EXP expression
""")
versioninfo("Added in 2.4")
typeinfo(int)
def p_expression_binop(self, p):
p[0] = func[p[2]](p[1],p[3])

decorate:
grammarrule(''expression : MINUS expression %prec UMINUS'')
versioninfo("Added in 1.5")
typeinfo(int)
def p_expression_uminus(self, p):
p[0] = -p[2]

decorate:
grammarrule(''expression : LPAREN expression RPAREN'')
versioninfo("Added in 1.1")
typeinfo(int)
def p_expression_group(self, p):
p[0] = p[2]

In a syntax highlighted editor that also looks *alot* cleaner than
beforehand. IMO, it also looks slightly cleaner than the @version - for
one very, very simple reason - if you have a very big decorator block,
for whatever reason, the "def" stands out clearly so you can skim where
the function starts - even without syntax highlighting.

My initial reaction to @decorators was "ugh!". However I was coming to
accept it as inevitable after reading the various arguments for/against
various syntaxes. *This* syntax strikes me personally as having
distinct advantages - largely for clarity of code, and solves the
"where *DOES* the function start?" question with large decorator
blocks.

My tuppenceworth... (I really like this version)
Michael.
--
Mi************@rd.bbc.co.uk
British Broadcasting Corporation, Research and Development
Kingswood Warren, Surrey KT20 6NP

This message (and any attachments) may contain personal views
which are not the views of the BBC unless specifically stated.

" Peter Hansen" < PE *** @ engcorp.com>在消息中写道

news:Pc ******************** @ powergate.ca ...
"Peter Hansen" <pe***@engcorp.com> wrote in message
news:Pc********************@powergate.ca...
<类Klass:
#init set

装饰:
staticmethod
def meth0(x):
返回x

class Klass:
# init set

decorate:
staticmethod
def meth0(x):
return x



美丽!!!看起来非常Pythonic。


这是我见过的第一个前缀语法提议,旨在解决技术和审美异议中的所有

。 />

没有神奇的标点 - 万岁!


- 保罗


BEAUTIFUL!!! Looks very Pythonic.

This is the first prefix syntax proposal I''ve seen that looks to address all
of the technical and esthetic objections.

And NO magic punctuation - hooray!

-- Paul


这篇关于尚未提及的装饰器语法(我想!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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