News for hackers. Stuff that matters.
Re:Python idiom for taking the single item from a list by jemfinch on Monday September 27, 2010 @04:43PM (#1733328)
> assert l and len(l) == 1

This is redundant: if a list's length is 1, then it's true in a boolean context.

Also, please stop naming your lists 'l'. On a vast array of fonts, it differs only in a few pixels from '1'. Use "L" instead :)

Re:Python idiom for taking the single item from a list by statictype on Monday September 27, 2010 @08:59PM (#1734192)
This is redundant: if a list's length is 1, then it's true in a boolean context.

In a boolean context, the list is true if the length is non-zero. This example and the one the article is about is for the case where you know the list to have exactly one element. Not zero and not more than one.

Re:Python idiom for taking the single item from a list by jemfinch on Tuesday September 28, 2010 @12:17AM (#1734543)
You are aware that asserting `len(L) == 1` excludes the possibility that it's empty, right? You know what "redundant" means, right?
Re:Python idiom for taking the single item from a list by statictype on Tuesday September 28, 2010 @01:51AM (#1734724)
if L is None then asserting `len(L)==1` throws an exception.

The `if L and` part safeguards against that.

Re:Python idiom for taking the single item from a list by jemfinch on Tuesday September 28, 2010 @01:46PM (#1736518)
And the point that was made in the other branch of this thread is that if the author intends to guard against None (who knows why?) then he should say, explicitly, "if L is not None". That's what PEP8 recommends precisely to avoid ambiguities such as these.
Re:Python idiom for taking the single item from a list by durin42 on Monday September 27, 2010 @04:54PM (#1733372)
He's defending against None with the assert l and len(l) bit, or at least that's what I'd assume.
Re:Python idiom for taking the single item from a list by swolchok on Monday September 27, 2010 @05:18PM (#1733468)
If he's defending against None, he should

    assert l is not None and len(l) == 1
Re:Python idiom for taking the single item from a list by dirtyaura on Tuesday September 28, 2010 @02:05AM (#1734744)
There's a better variable naming scheme for generic lists:

Use xs. If you have multiple lists use ys etc.

This has multiple benefits over L in terms of readability anad understandability as a single-item variable names can be made to match the list naming scheme:

  for x in xs: 
     for y in ys: 
        do_some_fancy_calculation(x,y)
Re:Python idiom for taking the single item from a list by j_baker on Monday September 27, 2010 @05:01PM (#1733404)
I wouldn't name a list l in real code. It was just the first thing that popped to mind. :-)

And as was mentioned the "assert l" part is defending against l being None. I suppose I could be more explicit by saying "assert l is not None and len(l) == 1".

Re:Python idiom for taking the single item from a list by jemfinch on Monday September 27, 2010 @05:26PM (#1733507)
Yes, you should definitely should be more explicit if that's what you intend to check: PEP8 makes that explicit.

I'm not sure why you'd defend against None anyway. Why defend against None, but not against 3.1459 or 4j or ''?

Re:Python idiom for taking the single item from a list by statictype on Monday September 27, 2010 @08:57PM (#1734182)
There's nothing wrong with `is l`. Sounds like you're just splitting hairs.

He's defending against None because calling __len__ on None results in an exception.

3.14159 also results in an exception but it's far more likely that the object passed was None than that it was a completely different type than the one expected.

Re:Python idiom for taking the single item from a list by jemfinch on Tuesday September 28, 2010 @04:24PM (#1737146)
> it's far more likely that the object passed was None than that it was a completely different type than the one expected.

FYI, None is a completely different type than the one expected.