PEP前:基于套件的关键字 [英] pre-PEP: Suite-Based Keywords

查看:68
本文介绍了PEP前:基于套件的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我称之为基于套件的关键字参数的预PEP。这里描述的

机制旨在作为thunk的补充。

请让我知道你的想法。


基于套件的关键字参数

-----------------------------


将复杂的参数传递给函数目前在Python中很尴尬。

例如,定义类属性的典型方法最终污染了

类的'带有属性'get / set方法的命名空间。通过允许在函数调用之后在套件中定义

关键字参数,

复杂的参数可以更简洁,更简单的方式传递。

示例

========


使用基于套件的关键字参数,代码

f(x = 1)


相当于


f():

x = 1


通常,函数调用后的套件会创建一个新范围。在此范围内创建的

绑定将作为关键字

参数传递给函数。


基于套件的关键字参数可以是混合常规论点:


f(1,2,y = 4):

x = 1


动机

==========


基于套件的关键字的一个动机是允许更清晰的定义

属性。目前,属性通常定义为此

示例:


C类(对象):

def getx(self) :

返回自我.__ x

def setx(自我,价值):

self .__ x = value

def delx(self):

del self .__ x

x = property(getx,setx,delx,我是''x''属性。 ;)


''getx'',''setx''和''delx''方法在

类的命名空间中定义即使只想把它们传递给''财产''。理想情况下,一个

会希望在自己的命名空间中定义这些方法。此外,如果代码的布局给了

视觉指示他们唯一的目的是在一个属性中使用它,那么在读取代码时它将是有用的。


使用基于套件的关键字参数,并且不对

''属性''类型进行任何更改,此代码可以写成:


类C(对象):

x = property():

doc ="我是''x''属性。 ;

def fget(个体经营):

返回自我.__ x

def fset(自我,价值):

self .__ x = value

def fdel(self):

del self .__ x


这里,''fget' ',''fset''和''fdel''不会成为类的方法,

并且在视觉上清楚它们只是'x'属性的方法。

此外,这个代码不易出错,因为每个方法的名称需要

只出现一次。


传递回调在其他情况下同样更容易制作

清洁剂:


setHandlers():

def成功():

打印''成功''

def failure():

打印''发生错误''

def apocalypse() :

打印''发生严重错误''


a = [1,3,2]

a.sort ():

def cmpfunc(x,y):

返回cmp(x,y)


没有的情况要求回调也可以更好地组织

使用基于套件的关键字。例如,这里是代码,因为它将使用Python编写



如果a:

x = 1

else:

x = 2

f(x = x)


阅读此代码后,达到''if''语句不知道它的目的是什么

--代码的布局并不表示''if''

语句正在计算一个参数到F''。此外,它需要一个绑定

,除了持有f参数之外没有任何意义,但是这个

绑定仍然存在于周围的其余功能。


以下是使用基于套件的关键字参数的相同代码


f():

如果:

x = 1

else:

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.

推荐答案

Brian Sabbey写道:
Brian Sabbey wrote:
C类(对象):
x = property():
doc = 我是''x''属性。
def fget(self):
返回自我.__ x
def fset(自我,价值):
self .__ x = value
def fdel(self):
del self .__ x
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




看完这个例子后,我想我会如果你的

提案没有在Python 2.5或2.6中使用,那就继续疯狂。定义像

这样的属性会很棒,只会更好。



After seeing this example, I think I''ll go on a killing spree if your
proposal doesn''t get used in Python 2.5 or 2.6. Defining properties like
that would be awesome, only better.


I_vote_yes(James):

I_understand_what_it_does = True

Makes_code_formatting_way_more_managable_in_tough_ cases = True

Makes_code_way_more_readable = True

To_cool = True


4月15日星期五2005年04:45 pm,Brian Sabbey写道:
I_vote_yes(James):
I_understand_what_it_does = True
Makes_code_formatting_way_more_managable_in_tough_ cases = True
Makes_code_way_more_readable = True
To_cool = True

On Friday 15 April 2005 04:45 pm, Brian Sabbey wrote:
这是我称之为基于套件的关键字参数的预PEP。这里描述的
机制旨在作为thunk的补充。
请让我知道你的想法。

基于套件的关键字参数
--- --------------------------

将复杂的参数传递给函数目前在Python中很难实现。
对于例如,定义类属性的典型方法最终会使用属性的get / set方法来污染类的命名空间。通过允许在函数调用之后在套件中定义
关键字参数,可以以更简洁,更简单的方式传递复杂的参数。

示例
== ======

使用基于套件的关键字参数,代码

f(x = 1)

相当于

f():
x = 1

通常,函数调用后的套件会创建一个新的范围。在此范围内创建的
绑定将作为关键字
参数传递给函数。

基于套件的关键字参数可以与常规参数混合使用:
f(1,2,y = 4):
x = 1

动机
==========

基于套件的关键字的一个动机是允许更清晰的属性定义。目前,属性通常定义如下:

C类(对象):
def getx(self):
返回self .__ x
def setx(self,value):
self .__ x = value
def delx(self):
del self .__ x
x = property(getx,setx,delx, 我是''x''属性。

'getx'',''setx''和''delx''方法在命名空间中定义即使只想将它们传递给''属性',所以
类。理想情况下,一个人希望在自己的命名空间中定义这些方法。此外,如果代码的布局给出了他们唯一目的是在一个属性中使用的可视指示,那么在阅读代码时会很有用。

使用套件 - 基于关键字参数,并且没有对
''property''类型进行任何更改,此代码可以写成:

C类(对象):
x = property( ):
doc ="我是''x''属性。"
def fget(self):
返回自我.__ x
def fset(self ,价值):
自我.__ x =价值
def fdel(self):
del self .__ x

这里,''fget'',''fset ''和''fdel''不会成为班级的方法,
并且在视觉上清楚它们只是'x''属性的方法。
此外,这段代码是因为每个方法的名称只需要出现一次就不会出错了。

在其他情况下传递回调也同样容易,并且清洁:

setHandlers():
def成功():打印''成功''
def失败():
打印' '发生错误''
def apocalypse():
打印''发生严重错误''

a = [1,3,2] a.sort():
def cmpfunc(x,y):
返回cmp(x,y)

不需要回调的情况也可以更好地组织<使用基于套件的关键字。例如,这里是代码,因为它当前将用Python编写:

如果a:
x = 1
否则:
x = 2
f(x = x)

当阅读这段代码时,会在不知道其目的是什么的情况下达到''if''语句 - 代码的布局不会表示''if''
语句正在计算''f'的参数。此外,它需要一个没有任何目的的绑定,除了持有f的参数,但这个
绑定仍然存在于周围功能的其余部分。
以下是使用基于套件的关键字参数的相同代码

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




是一个套件只有在当前语法中无法引入块时才会进入全局?


Kent



ISTM the syntax is ambiguous. How do you interpret
if f():
x = 1
?

Is a suite alllowed only when a block could not be introduced in the current syntax?

Kent


这篇关于PEP前:基于套件的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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