[CentralOH] Syntactic Sugar(, or sacharine(, or Lead acetate))?

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Fri Aug 12 22:49:18 CEST 2011

On Fri, 12 Aug 2011 12:39:11 -0400, Mark Erbaugh <mark at microenh.com> wrote:

> 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.

   " ... premature optimization is the root of all evil"[0]

Speed is important, but meaning is more important, 
so the question I would focus on is: 

    Does {min: 0} express what you _mean_ more clearly than {'min': 0}? 

Is using a function as a key, the distilled essence of what you 
mean? If[1] so, then it's likely a good thing. If you actually 
want to call the key, then {min: 0} is likely a good thing. 
If without using the function as the key, you have to use an 
eval(key) somewhere else, I would say that having the 
function as the key is likely a good thing. 
(There may be multiple good ways.) 

> One thing I didn't mention is that the dict I am using is for 
> the info parameter to SQLAlchemy's Column constructor. 
> The info parameter is designed to store application specific 
> information. In my case, I'm intending to use info to store 
> information needed to validate user input strings (from a web 
> page) destined for the database column. 

If you want the key to be a validator function, 
then by all means use the function as the key, 
as it is callable. _If_ I was doing something like that with 
the _keys_, I would expect all the keys to be functions. 

> The SQLAlchemy docs state that the info paramter must be a dict.

I would consider something like: 

    {validator_function: min, validator_args: [0]}

That sounds more like the simple direct essence of meaning, 
but I am ignorant of the larger context to make a choice. 
By the way, does your validator function need to return True 
or False? 

Regardless of the appropriateness of having a key be a 
function, it is fun to explore the speed of using such. 

Not all that is sweet[2] is good.

[0] https://secure.wikimedia.org/wikipedia/en/wiki/Optimization_(computer_science)#When_to_optimize
[1] I am not affirming antecendents. 
[2] https://secure.wikimedia.org/wikipedia/en/wiki/Lead(II)_acetate

More information about the CentralOH mailing list