URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 19 » [精华] Python 食谱--1.8:集中一组命名的条目
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
[精华] Python 食谱--1.8:集中一组命名的条目
Author: rings    Posted: 2004-04-26 23:24    Length: 6,497 byte(s)
[Original] [Print] [Top]
1.8 Collecting a Bunch of Named Items
集中一组命名的条目
Credit: Alex Martelli

1.8.1 Problem
问题

You want to collect a bunch of items together, naming each item of the bunch, and you find dictionary syntax a bit heavyweight for the purpose.
你想把一组条目集中在一起,命名他们中的每一项。并且你发现重要的字典语法可以达成这个目的

1.8.2 Solution
解决

Any (classic) class inherently wraps a dictionary, and we take advantage of this:
任何(经典)类本来就包装了一个字典,我们可以利用这个:
class Bunch:
def _ _init_ _(self, **kwds):
self._ _dict_ _.update(kwds)

Now, to group a few variables, create a Bunch instance:
现在,为了集中一些变量,建立一个Bunch实例:
point = Bunch(datum=y, squared=y*y, coord=x)

You can access and rebind the named attributes just created, add others, remove some, and so on. For example:
你能够访问和重新绑定刚刚被建立,命名的属性,增加其他的,移除一些等。例如:
if point.squared > threshold:
point.isok = 1

1.8.3 Discussion
讨论

Often, we just want to collect a bunch of stuff together, naming each item of the bunch; a dictionary's okay for that, but a small do-nothing class is even handier and is prettier to use.
经常地,我们仅仅想把一组元素集中在一起,为他们每一个命名。一个字典能够胜任它,但是一个小型的什么也不做的类更方便,更利于使用

A dictionary is fine for collecting a few items in which each item has a name (the item's key in the dictionary can be thought of as the item's name, in this context). However, when all names are identifiers, to be used just like variables, the dictionary-access syntax is not maximally clear:
集中一些元素,让每个元素都有一个名字,这对于字典来说不是难事(在这个上下文中,字典里元素的键能够被考虑作为元素的名字)。然而,当所有名字是标识符的时候,能够象变量一样被使用,访问字典的语法不在是非常清晰的了。

if point['squared'] > threshold

It takes minimal effort to build a little class, as in this recipe, to ease the initialization task and provide elegant attribute-access syntax:
花费更小的代价构造一个很小的类,就象在这个配方里, 更容易初始化任务并提供优雅的属性访问语法:

if bunch.squared > threshold

An equally attractive alternative implementation to the one used in the solution is:
一个同样有吸引力的可选择的实现在下列解决方案中被使用:

class EvenSimplerBunch:
def _ _init_ _(self, **kwds): self._ _dict_ _ = kwds

The alternative presented in the Bunch class has the advantage of not rebinding self._ _dict_ _ (it uses the dictionary's update method to modify it instead), so it will keep working even if, in some hypothetical far-future dialect of Python, this specific dictionary became nonrebindable (as long, of course, as it remains mutable). But this EvenSimplerBunch is indeed even simpler, and marginally speedier, as it just rebinds the dictionary.
在选择的Bunch类里表示有一个好处,不用重新绑定self._ _dict_ _(取而代之的是它使用字典的update方法去更改它)。所以它将继续工作,即使,在一些假定python遥远未来的方言中,这个特殊的字典变得不可绑定时(当然,只要它仍然可变)但是当它仅仅重绑定字典的时候EvenSimplerBunch确实很简单,并且更快。

It is not difficult to add special methods to allow attributes to be accessed as bunch['squared'] and so on. In Python 2.1 or earlier, for example, the simplest way is:
增加特殊的方法来允许属性被作为bunch['squared']来访问不是很困难。在Python2.1或更早版本,例如,最简单的方法是:
import operator

class MurkierBunch:
def _ _init_ _(self, **kwds):
self._ _dict_ _ = kwds
def _ _getitem_ _(self, key):
return operator.getitem(self._ _dict_ _, key)
def _ _setitem_ _(self, key, value):
return operator.setitem(self._ _dict_ _, key, value)
def _ _delitem_ _(self, key):
return operator.delitem(self._ _dict_ _, key)

In Python 2.2, we can get the same effect by inheriting from the dict built-in type and delegating the other way around:
在Python 2.2,我们能通过继承从字典内建类型得到相同的效果并且委托周围的其他方法:
class MurkierBunch22(dict):
def _ _init_ _(self, **kwds): dict._ _init_ _(self, kwds)
_ _getattr_ _ = dict._ _getitem_ _
_ _setattr_ _ = dict._ _setitem_ _
_ _delattr_ _ = dict._ _delitem_ _

Neither approach makes these Bunch variants into fully fledged dictionaries. There are problems with each梖or example, what is someBunch.keys supposed to mean? Does it refer to the method returning the list of keys, or is it just the same thing as someBunch['keys']? It's definitely better to avoid such confusion: Python distinguishes between attributes and items for clarity and simplicity. However, many newcomers to Python do believe they desire such confusion, generally because of previous experience with JavaScript, in which attributes and items are regularly confused. Such idioms, however, seem to have little usefulness in Python. For occasional access to an attribute whose name is held in a variable (or otherwise runtime-computed), the built-in functions getattr, setattr, and delattr are quite adequate, and they are definitely preferable to complicating the delightfully simple little Bunch class with the semantically murky approaches shown in the previous paragraph.
两种方法都不能使这些Bunch变量制造成完全的字典。每个方法或者例子都有问题, someBunch.keys的假设意味着什么?它引用返回键列表的方法,或者仅仅和someBunch['keys']相同吗?它明显更好的避免那样的混淆:为了清楚和简单,python区分属性和条目。然而,许多python新手相信他们期望那样的混淆,一般是因为以前JavaScript的经验。在JavaScript中,属性和条目被有规律的混淆。然而,那样的习惯用法似乎在python里没有什么用。为了偶然访问一个名字在变量里的属性(或者另外运行时的计算),内建的函数,getattr, setattr, 和delattr是足够了。对于显示在前面带有晦涩语义复杂化优美简单的小Bunch类的方法而言,他们是更优越的。
1.8.4 See Also
参考
The Tutorial section on classes.
[Original] [Print] [Top]
« Previous thread
与oracle的接口问题
Python 编 程
19
Next thread »
问一下python怎么念啊?
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:11:40, cost 0.052510976791382 ms.