[Tutor] how to unittest cli input
Danny Yoo
dyoo at hashcollision.org
Wed Oct 14 20:29:42 CEST 2015
>>> ######################
>>> def make_ask(f, l, p):
>>> d = {'Enter your first name: ' : f,
>>> 'Enter your last name: ' : l,
>>> 'Your mobile phone #: ' : p}
>>> return d.get
>>> ######################
>
>
> This is an example of a 'closure' is it not?
Yes, though I try not to use the word "closure" because it's a
technical distinction that isn't really that useful for beginners
because it focuses on the implementation details. That is, when we
say the word "closure", we're emphasizing the fact that it's a
combination of code and the data that it needs to resolve the code's
free variables. And for this discussion, all I really cared about was
that we needed something that knows how to be "called". Anything that
distracts from that point is something I'm actively trying to avoid,
at least at first.
We could try to make the code look clever by doing something like:
################################################
make_ask = lambda f, l, p: (lambda key: {'Enter your first name: ' :
f, 'Enter your last name: ' : l, 'Your mobile phone #: ' : p}[key])
################################################
But this would be death to the point I was trying to make. :P
More information about the Tutor
mailing list