目前,类和函数定义包含一个关键字 - class和class。或def - 后跟名称,标题,然后是代码。 代码被编译成一个对象,名称绑定到该对象。 该提议是允许名称为非名称(或罕见的名称) 令牌。在这种情况下,代码被编译,结果对象是 用作class / def表达式的值。 我选择非名称标记是@。它已经具有魔力 的力量,所以我们会给予它更多而不是引入另一个具有魔法力量的b / b 作为两个邪恶中的较小者。 br /> 用这个成语重写规范滥用lambda给出: myfunc = def @(* args): 返回总和(x + 1代表args) 换句话说,这与以下内容相同: def myfunc(* args ): 返回总和(x在x中为x + 1) 我们可以使用以下记录信息编写相同的循环: 总和(def @(arg): 打印" Bumping",arg 返回arg + 1 ( x)#''(''与def相同的缩进级别,结束定义 for x in stuff) 一个更有用的例子是不受欢迎的属性创建没有 混乱类命名空间: 类垃圾邮件(对象): myprop = property( fget = def @(self): 返回self._properties [''myprop''] , fset = def @(self,value): self._properties [' 'myprop''] = value , fdel = def @(self) del self._properties [''myprop''] , doc ="只是一个例子") 这看起来像滥用lambda案件,但这些都不是' 分配,它们是关键字参数。你可以不使用 关键字,但它并不显着更漂亮。 fget可以用 lambda完成,但其他人不能。 赋予clases相同的功能似乎是合理的事情 要做。它是对称的。如果匿名函数对象是好的,那么匿名类对象也应该是好的。 < mike - Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/ 独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。 I know, lambda bashing (and defending) in the group is one of the mostpopular ways to avoid writing code. However, while staring at some Ozcode, I noticed a feature that would seem to make both groups happy -if we can figure out how to avoid the ugly syntax. This proposal does away with the well-known/obscure "lambda"keyword. It gives those who want a more functional lambda what theywant. It doesn''t add any new keywords. It doesn''t add any new magiccharacters, though it does add meaning to an existing one. That couldbe replaced by a new magic token, or adding magic meaning to anon-magic token. It breaks no old code either way. I haven''t really worked out all the implications; I just wanted tothrow it out and see what everyone else thought about it. As aresult, the code examples tend to be ugly. As previously hinted, this feature is lifted from Oz. Currently, class and functions definitions consist of a keyword -either "class" or "def" - followed by a name, a header, then code. Thecode is compiled into an object, and the name is bound to that object. The proposal is to allow name to be a non-name (or rare name)token. In this case, the code is compiled and the resulting object isused as the value of the class/def expression. My choice for the non-name token is "@". It''s already got magicpowers, so we''ll give it more rather than introducing another tokenwith magic powers, as the lesser of two evils. Rewriting a canonical abuse of lambda in this idiom gives: myfunc = def @(*args):return sum(x + 1 for x in args) In other words, this is identical to: def myfunc(*args):return sum(x + 1 for x in args) We can write the same loop with logging information as: sum(def @(arg):print "Bumping", argreturn arg + 1(x) # ''('' at the same indent level as def, to end the definitionfor x in stuff) A more useful example is the ever-popular property creation withoutcluttering the class namespace: class Spam(object):myprop = property(fget = def @(self):return self._properties[''myprop''],fset = def @(self, value):self._properties[''myprop''] = value,fdel = def @(self)del self._properties[''myprop''],doc = "Just an example") This looks like the abuse of lambda case, but these aren''tassignments, they''re keyword arguments. You could leave off thekeywords, but it''s not noticably prettier. fget can be done with alambda, but the the others can''t. Giving clases the same functionality seems to be the reasonable thingto do. It''s symmetric. And if anonymous function objects are good,then anonymous class objects ought to be good as well. <mike--Mike Meyer <mw*@mired.org>http://www.mired.org/home/mwm/Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.推荐答案 Mike Meyer写道:Mike Meyer wrote:我对非名称标记的选择是@。它已经具备了魔法的力量,所以我们会给它更多而不是引入另一个具有魔法力量的象征,作为两个邪恶中较小的一个。 My choice for the non-name token is "@". It''s already got magic powers, so we''ll give it more rather than introducing another token with magic powers, as the lesser of two evils. 不行。你的改变的关键是没有给@ 带来意义(老实说,我更喜欢_),这是你从'改变''定义块'' a compound_stmt(funcdef)(参见 www.python.org/doc/current/ref/compound.html )到expression_stmt (expresion)。这个改变会让一些非常奇怪的东西,比如: 如果def _(x,y): 返回x ** 2 - y ** 2 (5,-5):#?!你怎么会立刻称之为''lambda-like''?[1] 打印''真'' 否则: 打印''false'' [1] - 是的,这通常是愚蠢的,但我只是指出了什么有什么 可能。 此外,Python的缩进只是不是那样的工作;强制要求 缩进之后def出现在前一行 (正如你在 你的例子中所做的那样,我不知道你是否打算用你的实际 语法保留它)不会解析正确 - 根据我的理解,tokenizer会为空格生成INDENT和 DEDENT令牌。 我个人最喜欢的是替换lambda。完全使用 表达式理解,使用<和>定界符。它看起来就像我们现有的列表和生成器理解一样,并且它没有使用 ''lambda''术语,这会使任何新手都迷失在Python的 在Lisp中的经验(至少它对我有用)。 g =< x ** 2 with(x)> g(1)== 1 基本上,我会重写Python语法,这样: lambda_form :: ="<"表达with parameter_list">" 最大的变化是parameter_list不再是可选的,所以 零参数expr-comps将被写为< expr with()>,这使得 a比< expr with>更有意义。 因为<<"和>在表达中并不含糊。这个 不应该使语法模棱两可。 with魔术词确实与bEP-343冲突(语义上,不是语法上),因此for可能 如果意义不太准确则适用。 Doesn''t work. The crux of your change isn''t introducing a meaning to @(and honestly, I prefer _), it''s that you change the ''define block'' froma compound_stmt (funcdef) (see www.python.org/doc/current/ref/compound.html) to an expression_stmt(expresion). This change would allow some really damn weird things, like: if def _(x,y):return x**2 - y**2(5,-5): # ?! How would you immediately call this ''lambda-like''?[1]print ''true''else:print ''false'' [1] -- yes, it''s generally stupid to, but I''m just pointing out what hasto be possible. Additionally, Python''s indenting Just Doesn''t Work Like That; mandatingan indent "after where the def came on the previous line" (as you do inyour example, I don''t know if you intend for it to hold in your actualsyntax) wouldn''t parse right -- the tokenizer generates INDENT andDEDENT tokens for whitespace, as I understand it. My personal favourite is to replace "lambda" entirely with an"expression comprehension", using < and > delimeters. It just lookslike our existing list and generator comprehensions, and it doesn''t use''lambda'' terminology which will confuse any newcomer to Python that hasexperience in Lisp (at least it did me). g = <x**2 with (x)>g(1) == 1 Basically, I''d rewrite the Python grammar such that:lambda_form ::= "<" expression "with" parameter_list ">" Biggest change is that parameter_list is no longer optional, sozero-argument expr-comps would be written as <expr with ()>, which makesa bit more sense than <expr with>. Since "<" and ">" aren''t ambiguous inside the "expression" state, thisshouldn''t make the grammar ambiguous. The "with" magic word doesconflict with PEP-343 (semantically, not syntactically), so "for" mightbe appropriate if less precise in meaning. Christopher Subich< sp ************* ***@block.subich.spam.com>写道:Christopher Subich <sp****************@block.subich.spam.com> writes:我个人最喜欢的是替换lambda。完全使用表达理解,使用<和> delimeters。 My personal favourite is to replace "lambda" entirely with an "expression comprehension", using < and > delimeters. 但是如何让你在 匿名函数中获得多个表达式呢? But how does that let you get more than one expression into theanonymous function? Christopher Subich写道:Christopher Subich wrote: g =< x ** 2 with(x)> g(1)== 1 lambda_form :: ="<"表达with parameter_list">" 最大的变化是parameter_list不再是可选的,所以零参数expr-comps将被写为< expr with()> ;, 比< expr与>更有意义。 因为<<"和>在表达中并不含糊。这个不应该使语法模棱两可。 with魔术词与PEP-343(语义上,不是语法上)发生冲突,因此for如果意义不太准确,可能是合适的。 g = <x**2 with (x)> g(1) == 1 Basically, I''d rewrite the Python grammar such that: lambda_form ::= "<" expression "with" parameter_list ">" Biggest change is that parameter_list is no longer optional, so zero-argument expr-comps would be written as <expr with ()>, which makes a bit more sense than <expr with>. Since "<" and ">" aren''t ambiguous inside the "expression" state, this shouldn''t make the grammar ambiguous. The "with" magic word does conflict with PEP-343 (semantically, not syntactically), so "for" might be appropriate if less precise in meaning. 解析器必须翻译的是什么样的恶作剧: < ; x ** 2与(x)><< x ** 3与(x)> 这是两个函数的比较,但它看起来像一个left- 移位函数,直到遇到第二个。然后 您需要回溯到班次并将其转换为一对 少量本品才能成功翻译。 --Scott David Daniels Sc *********** @ Acm.Org 这篇关于替代lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 10:04