具有索引的中文文章模板

1. 1.7 Dispatching Using a Dictionary

使用字典来分发

Credit: Dick Wall

1.1. 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.2. 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函数中是一类值(特别地,在一个字典里他们能够是值),把他们凑在一 起,让这个问题相当容易解决:

   1 animals = []
   2 number_of_felines = 0
   3 
   4 def deal_with_a_cat(  ):
   5     global number_of_felines
   6     print "meow"
   7     animals.append('feline')
   8     number_of_felines += 1
   9 
  10 def deal_with_a_dog(  ):
  11     print "bark"
  12     animals.append('canine')
  13 
  14 def deal_with_a_bear(  ):
  15     print "watch out for the *HUG*!"
  16     animals.append('ursine')
  17 
  18 tokenDict = {
  19     "cat": deal_with_a_cat,
  20     "dog": deal_with_a_dog,
  21     "bear": deal_with_a_bear,
  22     }
  23 
  24 # Simulate, say, some words read from a file
  25 words = ["cat", "bear", "cat", "dog"]
  26 
  27 for word in words:
  28     # Look up the function to call for each word, then call it
  29     functionToCall = tokenDict[word]
  30     functionToCall(  )
  31     # You could also do it in one step, tokenDict[word](  )

1.3. 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.4. 1.7.4 See Also

参考

The Library Reference section on mapping types; the Reference Manual section on bound and unbound methods.