[_] Python Question
jan.grant at bristol.ac.uk
jan.grant at bristol.ac.uk
Thu Dec 17 17:19:17 GMT 2009
On Thu, 17 Dec 2009, Matt Hamilton wrote: > > On 17 Dec 2009, at 16:35, Rick Edwards wrote: > > > 2009/12/17 Matt Hamilton matth at netsight.co.uk > > > >> > >> If the case is likely that bar will be in attributes you could do do > >> the 'do it, then ask forgiveness' route: > >> > >> for foo,bar in attribute_data: > >> try: > >> attributes[bar].append(foo) > >> except KeyError: > >> attributes[bar] = [foo,] > >> > > > > Tsk, using exception handling to control logical flow like this, > > shame on > > you! > > http://wiki.sheep.art.pl/Ask%20for%20Forgiveness Might be faster; I'm not sure it reads as naturally (and the reason I like Python is that it tends to avoid clunky idioms*). If speed's _that_ essential there are other languages - I'm not sure that's really a valid excuse. jan * the exception being the overloading of "else" as a keyword in a way that was invented by a non-native English speaker. -- jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/ Tel +44 (0)117 3317661 http://ioctl.org/jan/ Ceci n'est pas une pipe |