URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 19 » [精华] Python 食谱--1.7:使用字典来分发
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.7:使用字典来分发
[精华] Python 食谱--1.7:使用字典来分发 - rings [2004-04-25 17:04 | 4,454 byte(s)]
 
Re: Python 食谱--1.7:使用字典来分发 - zoomquiet [2004-04-26 09:24 | 166 byte(s)]
 
Re: Python 食谱--1.7:使用字典来分发 - rings [2004-04-26 23:28 | 72 byte(s)]
 
Re: Python 食谱--1.7:使用字典来分发 - limodou [2004-04-26 10:33 | 46 byte(s)]
 
Subject: [精华] Python 食谱--1.7:使用字典来分发
Author: rings    Posted: 2004-04-25 17:04    Length: 4,454 byte(s)
[Original] [Print] [Top]
1.7 Dispatching Using a Dictionary
使用字典来分发

Credit: Dick Wall
1.7.1 Problem
问题

You need to execute appropriate pieces of code in correspondence with the value of
some control variable梩he kind of problem that in some other languages you might
approach with a case, switch, or select statement.
你需要执行一段合适的代码去和一些控制变量的值来通信。问题在于在一些其他语言里,你可能要
处理case,switch, 或者select语句

1.7.2 Solution
解决
Object-oriented programming, thanks to its elegant concept of dispatching, does away
with many (but not all) such needs. But dictionaries, and the fact that in Python
functions are first-class values (in particular, they can be values in a dictionary),
conspire to make the problem quite easy to solve:
面向对象的编程,由于它的优雅的分发概念,可以满足许多(不是全部)那样的需要。
但是字典,实际在python函数中是一类值(特别地,在一个字典里他们能够是值),把他们凑在一
起,让这个问题相当容易解决:

animals = []
number_of_felines = 0

def deal_with_a_cat( ):
global number_of_felines
print "meow"
animals.append('feline')
number_of_felines += 1

def deal_with_a_dog( ):
print "bark"
animals.append('canine')

def deal_with_a_bear( ):
print "watch out for the *HUG*!"
animals.append('ursine')

tokenDict = {
"cat": deal_with_a_cat,
"dog": deal_with_a_dog,
"bear": deal_with_a_bear,
}

# Simulate, say, some words read from a file
words = ["cat", "bear", "cat", "dog"]

for word in words:
# Look up the function to call for each word, then call it
functionToCall = tokenDict[word]
functionToCall( )
# You could also do it in one step, tokenDict[word]( )

1.7.3 Discussion
讨论

The basic idea behind this recipe is to construct a dictionary with string (or other)
keys and with bound methods, functions, or other callables as values. During
execution, at each step, use the string keys to select which method or function to
execute. This can be used, for example, for simple parsing of tokens from a file
through a kind of generalized case statement.
这个配方后面基本的思想是,构造一个字典使用string(或其他)作为键并且绑定了方法,函数,
或者其他可以调用的值。在执行期间,每一步,使用string键选择去执行那一个方法或者函数它能
被使用,例如,通过普通的case语句从一个文件里简单的解析标号。

It's embarrassingly simple, but I use this technique often. Instead of functions, you
can also use bound methods (such as self.method1) or other callables. If you use
unbound methods (such as class.method), you need to pass an appropriate object as the
first actual argument when you do call them. More generally, you can also store
tuples, including both callables and arguments, as the dictionary's values, with
diverse possibilities.
虽然那是比较难堪的简化,但是我经常使用这种技术。不仅是函数,你也能使用绑定方法(如self
.method1)或者其他可调用的对象。如果你使用非绑定方法(如,class.method)当你调用他们的
时候,你需要传递一个合适的对象作为第一个实参。
更普遍的是,你也能存储tuple作为字典的值,tuple可以包括可调用对象和参数,让它更富变化。


I primarily use this in places where in other languages I might want a case, switch,
or select statement. I also use it to provide a poor man's way to parse command files
(e.g., an X10 macro control file).
我主要使用它在一些其他语言中,我象使用case,switch, 或者select语句的地方。
我也使用它去提供一个可怜人的方法去解析命令文件(如一个X10宏控制我文件)
1.7.4 See Also
参考
The Library Reference section on mapping types; the Reference Manual section on bound
and unbound methods.

[Original] [Print] [Top]
Subject: Re: Python 食谱--1.7:使用字典来分发
Author: zoomquiet    Posted: 2004-04-26 09:24    Length: 166 byte(s)
[Original] [Print] [Top]
强哪!
不过 CookBook 的版权问题,是自由的?

那未,建议去 CZUG 开辟Wiki 的"Python 食谱"文档项目,可以更加集中的吸引同志们是也乎?
----
Time is unimportant, only life important!
[Original] [Print] [Top]
Subject: Re: Python 食谱--1.7:使用字典来分发
Author: limodou    Posted: 2004-04-26 10:33    Length: 46 byte(s)
[Original] [Print] [Top]
是个好主意。这里毕竟不是一个存放文档的好地方。
----
我的Blog(http://limodou.donews.net/limodou)
[Original] [Print] [Top]
Subject: Re: Python 食谱--1.7:使用字典来分发
Author: rings    Posted: 2004-04-26 23:28    Length: 72 byte(s)
[Original] [Print] [Top]
我也不知道这本书的版权是怎么样的?似乎不是自由的?所以,先在这里发发看。
[Original] [Print] [Top]
« Previous thread
使用[code]和[/code] UBB标记来包括代码
Python 编 程
19
Next thread »
[精华] Python 食谱--1.6:在字典里把每一个键同多个值关联起来
     

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.050014019012451 ms.