[Tutor] how to unittest cli input
Peter Otten
__peter__ at web.de
Thu Oct 15 09:52:31 CEST 2015
Alex Kleider wrote:
> On 2015-10-14 12:27, Peter Otten wrote:
>> 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"
>
> Thank you, Peter, for your continued efforts to explain.
> It is all getting pretty convoluted for my poor brain!
> It took a very long time for me to figure out what the
> class WontTell was all about.
Sorry about that digression. The example would work with any old dict
>>> get, set_d = make_ask("John", "Doe", "123")
>>> get("Enter your last name: ")
'Doe'
>>> set_d({"foo": "bar"})
>>> get("Enter your last name: ") is None
True
>>> get("foo")
'bar'
but that way the effect of rebinding d (which is what happens) is the same
as replacing the data in the dict initially bound to d.
> I probably should follow Danny Yoo's advice and not concern
> myself with this but my curiosity is roused.
More information about the Tutor
mailing list