<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://programmingexamples.net/w/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://programmingexamples.net/w/index.php?action=history&amp;feed=atom&amp;title=Python%2FDictAsSwitch</id>
		<title>Python/DictAsSwitch - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://programmingexamples.net/w/index.php?action=history&amp;feed=atom&amp;title=Python%2FDictAsSwitch"/>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/w/index.php?title=Python/DictAsSwitch&amp;action=history"/>
		<updated>2026-04-19T17:46:32Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.5</generator>

	<entry>
		<id>http://programmingexamples.net/w/index.php?title=Python/DictAsSwitch&amp;diff=265&amp;oldid=prev</id>
		<title>Griswolf: format</title>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/w/index.php?title=Python/DictAsSwitch&amp;diff=265&amp;oldid=prev"/>
				<updated>2010-06-29T04:13:15Z</updated>
		
		<summary type="html">&lt;p&gt;format&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 04:13, 29 June 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Python|&amp;lt;font size='-2'&amp;gt;''back to Python examples''&amp;lt;/font&amp;gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Python|&amp;lt;font size='-2'&amp;gt;''back to Python examples''&amp;lt;/font&amp;gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;----&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;----&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In many languages, there is a switch or case statement. Python appears not to have that, so you will see a lot of if/elif/elif/.../else . There is a better way using a dict.&amp;lt;br /&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In many languages, there is a switch or case statement. Python appears not to have that, so you will &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;often &lt;/ins&gt;see a lot of &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;code&amp;gt;&lt;/ins&gt;if/elif/elif/.../else&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/code&amp;gt; blocks&lt;/ins&gt;. There is a better way using a dict.&amp;lt;br /&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;For concreteness, lets mimic this C code:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;For concreteness, lets mimic this C code:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Griswolf</name></author>	</entry>

	<entry>
		<id>http://programmingexamples.net/w/index.php?title=Python/DictAsSwitch&amp;diff=264&amp;oldid=prev</id>
		<title>Griswolf: create example</title>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/w/index.php?title=Python/DictAsSwitch&amp;diff=264&amp;oldid=prev"/>
				<updated>2010-06-29T04:11:28Z</updated>
		
		<summary type="html">&lt;p&gt;create example&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Python|&amp;lt;font size='-2'&amp;gt;''back to Python examples''&amp;lt;/font&amp;gt;]]&lt;br /&gt;
----&lt;br /&gt;
In many languages, there is a switch or case statement. Python appears not to have that, so you will see a lot of if/elif/elif/.../else . There is a better way using a dict.&amp;lt;br /&amp;gt;&lt;br /&gt;
For concreteness, lets mimic this C code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int count_of_bs = 0&lt;br /&gt;
switch(a) {&lt;br /&gt;
  case 'a': call_a(a); break;&lt;br /&gt;
  case 'b': ++count_of_bs; break&lt;br /&gt;
  default: call_other(a); break;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Here it is in Python (with function definitions and a few quick tests)&lt;br /&gt;
=== switchExample.py ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
count_of_bs = 0&lt;br /&gt;
# these functions would have to be defined in any case.&lt;br /&gt;
def call_a(a):&lt;br /&gt;
  print 'a called with %s'%str(a)&lt;br /&gt;
&lt;br /&gt;
def call_other(a):&lt;br /&gt;
  print 'otherwise, with %s'%str(a)&lt;br /&gt;
&lt;br /&gt;
#this function is 'overhead' to make the switch statement work&lt;br /&gt;
def do_b(a):&lt;br /&gt;
  global count_of_bs&lt;br /&gt;
  count_of_bs += 1&lt;br /&gt;
&lt;br /&gt;
# this is the declaration of the switch&lt;br /&gt;
switch = {&lt;br /&gt;
  'a':call_a,&lt;br /&gt;
  'b': do_b,&lt;br /&gt;
  }&lt;br /&gt;
# run some test cases. Note that we handle non-scalars just fine&lt;br /&gt;
print ('count_of_bs %s'%count_of_bs)&lt;br /&gt;
for tc in ['b', 'a','b','c','b','alphabet',(99,'bottles','beer'),'b']:&lt;br /&gt;
  # this is how you use it&lt;br /&gt;
  switch.get(tc,call_other)(tc)&lt;br /&gt;
print ('count_of_bs %s'%count_of_bs)&lt;br /&gt;
  &lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;Output:&lt;br /&gt;
count_of_bs 0&lt;br /&gt;
a called with a&lt;br /&gt;
otherwise, with c&lt;br /&gt;
otherwise, with alphabet&lt;br /&gt;
otherwise, with (99, 'bottles', 'beer')&lt;br /&gt;
count_of_bs 4&lt;br /&gt;
&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
[[Python|&amp;lt;font size='-2'&amp;gt;''back to Python examples''&amp;lt;/font&amp;gt;]]&lt;/div&gt;</summary>
		<author><name>Griswolf</name></author>	</entry>

	</feed>