[CentralOH] Syntactic Sugar (or sacharine)?

Mark Erbaugh mark at microenh.com
Fri Aug 12 18:39:11 CEST 2011

I'm using a dict to store some parameters. I accidentally initialized the dict with {min:0} rather than {'min':0}, but the former worked as the built-in function min is hashable and thus suitable as a key. This saves me from having to type quotes in when I retrieve the value  (i.e. info[min] rather than info['min']). I'm thinking it's marginally faster as there's no string processing involved.  The only downside I can come up with is that the min function is not being used as intended and it could be confusing.

The same principle allows you to create objects to be used as dictionary keys to make the [key] lookup syntax cleaner.

What do people think about this?

class A: pass
class B: pass

d = {A:'value for A', B:'value for B'}


More information about the CentralOH mailing list