I am new to python. I have a few questions coming from an armature!
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Aug 17 02:07:38 EDT 2016
On Wednesday 17 August 2016 04:46, alister wrote:
> > squared_plus_one_list = map(lambda x: x**2 + 1, some_list)
>
> probably the cleanest example I have seen so far, & I still cant see the
> point
Hmmm. Well, let's have a look at some analogies with other kinds of values.
Out of each pair of examples, *in general* would you prefer (A) or (B)?
I realise that there are occasions where we might deliberate choose to assign
an intermediate value to its own variable, but all else being equal, which
would you prefer?
#A
alist = []
alist.append(2)
alist.append(4)
alist.append(8)
process(alist)
#B
process([2, 4, 8])
#A
value = 0
for i in range(100):
value += 1
process(value)
#B
process(100)
#A
tmp = get_some_string()
s = tmp[1]
s += tmp[2]
s += tmp[3]
process(s)
#B
process(get_some_string()[1:4])
#A
def callback(btn):
return btn.do_the_thing(42) or default
the_button.setcommand(callback)
process(the_button)
#B
the_button.setcommand(lambda btn: btn.do_the_thing(42) or default)
process(the_button)
If you find yourself preferring B, B, B, A, you might ask yourself what makes a
function different that you prefer to keep temporary functions around where
they're not needed.
--
Steve
More information about the Python-list
mailing list