[Python-ideas] Syntax for passing lambdas to functions
Ryan
rymg19 at gmail.com
Fri Feb 28 03:47:54 CET 2014
Last option's way more likely. I made that mistake about 2000 times moving over from C++.
Andrew Barnert <abarnert at yahoo.com> wrote:
>On Feb 27, 2014, at 13:24, Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Fri, Feb 28, 2014 at 8:14 AM, Steven D'Aprano
><steve at pearwood.info> wrote:
>>>> class A: ...
>>>> a = A()
>>>> l = [0]
>>>>
>>>> for a.x in [1, 2, 3]: ...
>>>> for l[0] in [1, 2, 3]: ...
>>>
>>> I wonder whether that is deliberate feature, or an accident of the
>way
>>> the syntax works. It does seem to be pretty useless though -- why
>are
>>> you using an attribute or list as a loop variable?
>>
>> I would say it's no accident that the for loop can accept any lvalue.
>> That's how we can do this, which I'm sure you'll agree is *extremely*
>> useful:
>>
>> for x,y in [(1,2), (3,4), (5,6)]: ...
>>
>> Assigning to the tuple works beautifully there (especially with
>> enumerate or zip). Being able to assign to other complex targets is a
>> bonus that you'll probably never come across (I can imagine someone
>> might have a use for "for self.blah in ...", but can't concoct any
>> right now)
>
>Something I've seen in real code (not _good_ code, but actually
>deployed):
>
> for self.index in range(len(self.values)):
> if self.values[self.index] == spam:
> break
> else:
> self.index = None
>
>Apparently someone didn't want to catch the exception from list.index.
>(Or, more likely, they were trying to write C code in Python.)
>_______________________________________________
>Python-ideas mailing list
>Python-ideas at python.org
>https://mail.python.org/mailman/listinfo/python-ideas
>Code of Conduct: http://python.org/psf/codeofconduct/
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140227/ed650e27/attachment-0001.html>
More information about the Python-ideas
mailing list