News for hackers. Stuff that matters.
Re:Python has brought computer programming to a vast new aud... by duckerude on Thursday July 19, 2018 @05:59PM (#17570705)

  sum(x > 3 for x in iterable)
True == 1, False == 0, so this counts how many items in iterable are greater than 3.

But I think that's the only non-dirty way I've used it, and if that wasn't possible a trivial count() function could do the job.

If I remember correctly, bool subclasses int mostly for compatibility with pre-existing conventions. People were already defining their own "True = 1" and "False = 0" constants before they became part of the language.

Re:Python has brought computer programming to a vast new aud... by cma on Thursday July 19, 2018 @10:48PM (#17572077)
If it wasn't possible you could still do:

    sum(x > 3 for int(x) in iterable)
And avoid a class of errors in other places.
Re:Python has brought computer programming to a vast new aud... by duckerude on Friday July 20, 2018 @02:31AM (#17572936)
That code is invalid, because you're using "int(x)" as an assignment target. What would you expect it to do?
Re:Python has brought computer programming to a vast new aud... by cma on Friday July 20, 2018 @06:27PM (#17579504)
sorry:

    sum(int(x) > 3 for x in iterable)
Re:Python has brought computer programming to a vast new aud... by duckerude on Tuesday July 24, 2018 @08:26AM (#17600360)
That works better if you expect things in iterable that aren't ints, but it's still summing bools. int(x) > 3 returns a bool, so in the end it's still doing something similar to

  sum([True, False, False, True, True, False])
But if bools didn't implicitly behaves like ints, but could still be converted to ints, you could do this:

  sum(int(x > 3) for x in iterable)