"1, 2, 4, 8, 16 ".split( ",") 

is an instruction to a string literal to return the substrings delimited by the given separator (or, by default, arbitrary runs of white space). 

join() is a string method because in using it you are telling the separator string to iterate over a sequence of strings and insert itself between adjacent elements. This method can be used with any argument which obeys the rules for sequence objects, including any new classes you might define yourself. Similar methods exist for bytes and bytearray objects. 

## 3.9 How fast are exceptions? 

A try/except block is extremely efficient if no exceptions are raised. Actually catching an exception is expensive. In versions of Python prior to 2.0 it was common to use this idiom: 

try : value=mydict[key] except KeyError : mydict[key] =getvalue(key) value=mydict[key] 

This only made sense when you expected the dict to have the key almost all the time. If that wasn’t the case, you coded it like this: 

if key in mydict: value=mydict[key] else : value=mydict[key] =getvalue(key) 

For this specific case, you could also use$_{value = dict.setdefault(key, getvalue(key))}$ , but only if the$_{getvalue()}$ call is cheap enough because it is evaluated in all cases. 

## 3.10 Why isn’t there a switch or case statement in Python? 

You can do this easily enough with a sequence of$_{if... elif... elif... else}$ . There have been some proposals for switch statement syntax, but there is no consensus (yet) on whether and how to do range tests. See PEP 275 for complete details and the current status. 

For cases where you need to choose from a very large number of possibilities, you can create a dictionary mapping case values to functions to call. For example: 

def function_1 (...): ... functions={ 'a': function_1, 'b': function_2, 'c':self.method_1,...} func=functions[value] func() 

For calling methods on objects, you can simplify yet further by using the$_{getattr()}$ built-in to retrieve methods with a particular name: 

def visit_a(self,...): ... ... 

(continues on next page) 

