[Tutor] how to unittest cli input
Peter Otten
__peter__ at web.de
Wed Oct 14 21:27:37 CEST 2015
Alex Kleider wrote:
> On 2015-10-13 14:44, Alex Kleider wrote:
>> On 2015-10-13 12:11, Danny Yoo wrote:
>>
>>
>>> ######################
>>> 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?
It does not make big difference, but I would call the return value "bound
method" rather than "closure". For me closure implies access to the local
namespace of the enclosing function, e. g.
def make_ask(f, l, p):
d = {'Enter your first name: ' : f,
'Enter your last name: ' : l,
'Your mobile phone #: ' : p}
def get(key):
return d.get(key)
return get
Here d is looked up when get() is invoked. Let's make a modification to
demonstrate that the current binding of d is used:
>>> def make_ask(f, l, p):
... d = {'Enter your first name: ' : f,
... 'Enter your last name: ' : l,
... 'Your mobile phone #: ' : p}
... def get(key):
... return d.get(key)
... def set_d(new_d):
... nonlocal d
... d = new_d
... return get, set_d
...
>>> get, set_d = make_ask(*"abc")
>>> get("Enter your first name: ")
'a'
>>> class WontTell:
... def get(self, key): return "won't tell"
...
>>> set_d(WontTell())
>>> get("Enter your first name: ")
"won't tell"
More information about the Tutor
mailing list