f(1,2,y = 4): a.sort():以下是使用基于套件的关键字参数的相同代码 f():如果a: x = 1 否则: x = 2 阅读此代码时,如果有人愿意,可以轻松跳过调用''f'所涉及的所有内容。由于该套件具有自己的命名空间,因此不必担心该套件会创建一些稍后在函数中重要的绑定。 Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class''s namespace with the property''s get/set methods. By allowing keyword arguments to be defined in a suite following a function call, complicated arguments can be passed in a cleaner, easier way. Examples ======== Using suite-based keyword arguments, the code f(x = 1) is equivalent to f(): x = 1 In general, a suite following a function call creates a new scope. The bindings created in this scope get passed to the function as keyword arguments. Suite-based keyword arguments can be mixed with regular arguments: f(1, 2, y = 4): x = 1 Motivation ========== One motivation for suite-based keywords is to allow cleaner definitions of properties. Currently, properties are typically define as in this example: class C(object): def getx(self): return self.__x def setx(self, value): self.__x = value def delx(self): del self.__x x = property(getx, setx, delx, "I''m the ''x'' property.") The ''getx'', ''setx'', and ''delx'' methods get defined in the namespace of the class even though one wants only to pass them to ''property''. Ideally, one would want these methods to be defined in their own namespace. Also, it would be helpful when reading the code if the layout of the code gave visual indication that their only purpose is to be used in a property. Using suite-based keyword arguments, and without any changes to the ''property'' type, this code can be written as: class C(object): x = property(): doc = "I''m the ''x'' property." def fget(self): return self.__x def fset(self, value): self.__x = value def fdel(self): del self.__x Here, ''fget'', ''fset'' and ''fdel'' do not wind up as methods of the class, and it is visually clear that they are methods only for the ''x'' property. Also, this code is less bug-prone since the name of each method need appear only once. Passing callbacks in other situations is made similarly easier and cleaner: setHandlers(): def success(): print ''success'' def failure(): print ''an error has occured'' def apocalypse(): print ''a serious error has occured'' a = [1,3,2] a.sort(): def cmpfunc(x,y): return cmp(x,y) Situations that do not require callbacks can also be better organized using suite-based keywords. For example, here is code as it would currently be written in Python: if a: x = 1 else: x = 2 f(x=x) When reading this code, one reaches the ''if'' statment without knowing what its purpose is-- layout of the code does not indicate that the ''if'' statement is calculating an argument to ''f''. Also, it requires a binding that serves no purpose other than to hold an argument to ''f'', yet this binding persists for the rest of the surrounding function. Here is the same code using suite-based keyword arguments f(): if a: x = 1 else: x = 2 When reading this code, it is easy to skip over everything that is involved in calling ''f'', if one so desires. Since the suite has its own namespace, one does not have to worry that the suite creates some bindings that will be important later in the function. - James Stroud,博士 加州大学洛杉矶分校基因组学和蛋白质组学研究所 专栏951570 洛杉矶,CA 90095 http:// www。 jamesstroud.com/ Brian Sabbey写道:Brian Sabbey wrote:这是我称之为基于套件的预PEP关键字参数"。这里描述的机制旨在作为thunk的补充。请让我知道你的想法。 基于套件的关键字参数 --- -------------------------- 将复杂的参数传递给函数目前在Python中很尴尬。例如,定义类属性的典型方法最终会使用属性的get / set方法来污染类的命名空间。通过允许在函数调用之后在套件中定义关键字参数,可以以更简洁,更简单的方式传递复杂的参数。 示例 == ====== 使用基于套件的关键字参数,代码 f(x = 1) 相当于 f(): x = 1 Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class''s namespace with the property''s get/set methods. By allowing keyword arguments to be defined in a suite following a function call, complicated arguments can be passed in a cleaner, easier way. Examples ======== Using suite-based keyword arguments, the code f(x = 1) is equivalent to f(): x = 1 ISTM语法不明确。你怎么解释 如果f(): x = 1 ? 是一个套件只有在当前语法中无法引入块时才会进入全局? KentISTM the syntax is ambiguous. How do you interpretif f():x = 1?Is a suite alllowed only when a block could not be introduced in the current syntax?Kent 这篇关于PEP前:基于套件的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 21:16