本文介绍了装饰器语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我投票赞成


def f():[synchronized,classmethod]

(函数体)


这是向后兼容的(Python< = 2.3 raise

SyntaxError),看起来比@好得多。


唯一的问题是你不能单行一个

装饰功能。你不能用@符号来表示@ b $ b,所以这不是什么大问题。


- 康奈利


__________________________________

你是Yahoo!?

新的和改进的Yahoo!邮件 - 100MB免费存储空间!

推荐答案




我几乎可以肯定,没有费心去检查,这个

是已经讨论和拒绝的众多提案中的一个,

如果没别的,只是因为它只能起作用的简单原因

很好地当你有一个简短的定义,例如你的例子

(带有f())而不是很好用一个更长的名字

和参数列表。不可避免地你需要将

装饰器部分拆分到第二行,要么需要一个

反斜杠(丑陋)或更改你关于它的第二个评论

使用以前的版本引发了一个SyntaxError(因为它然后
匹配有效但无意义的代码)。


无论如何,在返回之前检查过去的讨论

再次使用相同的参数。


为了记录,@语法是卑鄙的,可怕的是

非Pythonic,恕我直言,而且我真的希望永远不要阅读

使用它的代码。 : - (


(而我的首选语法是def f()[classmethod]:,FWIW)


-Peter



I''m nearly certain, without bothering to check, that this
was one of the many proposals already discussed and rejected,
if nothing else for the simple reason that it only works
nicely when you have a short definition such as your example
(with "f()") and not nicely at all with a much longer name
and argument list. Inevitably you would need to split the
decorator part onto a second line, either requiring a
backslash (ugly) or changing your second comment about it
raising a SyntaxError with previous versions (because it
then matches valid though meaningless code).

Anyway, check the past discussions before going back around
the same arguments yet again.

For the record, the @ syntax is despicable and horribly
un-Pythonic, IMHO, and I really hope never to read code that
uses it. :-(

(And my preferred syntax was "def f() [classmethod]:", FWIW)

-Peter





同意。我也喜欢你喜欢的语法 - 清晰,可读,没有

令人眼花缭乱的符号。


对Java和C#的引用作为使用@

的理由或理由让我想起我在缅因州生活的时候。当我抱怨时关于

司机有多糟糕,不可避免的回应是我们没有像马萨诸塞州的司机那样糟糕!


如果这是一些冲动的前进,只是选择一些东西并继续,

我想我们正在看到另一个反击正在制作。


我担心



Agreed. And I also like your preferred syntax - clear, readable, no
eye-jarring symbols.

The references to Java and C# as justification or rationale for using @
remind me of when I lived in Maine. When I complained about how bad the
drivers were, inevitably the response was "we''re not as bad as the drivers
in Massachusetts!"

If this was some impulsive lurch forward to just pick something and proceed,
I think we are seeing another backtick in the making.

I fear that



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

10-21 23:39