News for hackers. Stuff that matters.
Re:Go Bloviations by psykotic on Tuesday September 25, 2012 @02:04AM (#4568886)
> Python has this problem too

Python has had conditional expressions since version 2.5. Dumb example:

    def count(xs, p):
        return sum(1 if p(x) else 0 for x in xs)
Re:Go Bloviations by bdr on Tuesday September 25, 2012 @03:42AM (#4569140)
Side note: you can simply use sum(p(x)) for x in xs), assuming p is a predicate. It's a neat trick, though a bit slower than your ternary version.
Re:Go Bloviations by bjourne on Tuesday September 25, 2012 @04:15AM (#4569223)
Another way: len(filter(p, xs))
Re:Go Bloviations by daeken on Tuesday September 25, 2012 @04:23AM (#4569245)
With the sum approach the data set never exists in memory, but is generated as needed; not a big deal for a small set, but it can add up.
Re:Go Bloviations by lclarkmichalek on Tuesday September 25, 2012 @05:00AM (#4569349)
And as in python3, filter returns a generator, not a list, the len call will fail altogether.
Re:Go Bloviations by bjourne on Tuesday September 25, 2012 @08:12AM (#4569898)
Yes, I'm aware of that. But my philosophy is to always try to conserve keystrokes wherever possible. Premature optimization is the root of all evil and all that. :) For the same reason I prefer to use dict.items() over dict.iteritems() and range() over xrange() and so on.
Re:Go Bloviations by slurgfest on Tuesday September 25, 2012 @09:42AM (#4570259)
This is a simple enough case that I'm not sure it matters, but I think the sum version is simpler to read. Generator expressions are quite powerful if you want to write Python which is more functional-flavored.
Re:Go Bloviations by rat87 on Tuesday September 25, 2012 @10:20AM (#4570439)
I disagree, I think filter/length models the meaning better.
Re:Go Bloviations by rat87 on Tuesday September 25, 2012 @10:18AM (#4570431)
I guess technically True==1 and False==0 in python and it is considered "pythonic" but personally I think it's "ugly".
Re:Go Bloviations by bdr on Tuesday September 25, 2012 @05:11PM (#4572685)
I wouldn't even say it's Pythonic. As a reader, I would prefer len+filter (or ifilter). That's the most semantically clear.
Re:Go Bloviations by rat87 on Wednesday September 26, 2012 @05:13AM (#4574781)
I also prefer length + filter but while I dislike the style I think it is still considered pythonic.

The pep for adding bools to python: http://www.python.org/dev/peps/pep-0285/

    4) Should we strive to eliminate non-Boolean operations on bools
       in the future, through suitable warnings, so that for example
       True+1 would eventually (in Python 3000) be illegal?

    => No.

       There's a small but vocal minority that would prefer to see
       "textbook" bools that don't support arithmetic operations at
       all, but most reviewers agree with me that bools should always
       allow arithmetic operations.

    6) Should bool inherit from int?

    => Yes.

       In an ideal world, bool might be better implemented as a
       separate integer type that knows how to perform mixed-mode
       arithmetic.  However, inheriting bool from int eases the
       implementation enormously (in part since all C code that calls
       PyInt_Check() will continue to work -- this returns true for
       subclasses of int).  Also, I believe this is right in terms of
       substitutability: code that requires an int can be fed a bool
       and it will behave the same as 0 or 1.  Code that requires a
       bool may not work when it is given an int; for example, 3 & 4
       is 0, but both 3 and 4 are true when considered as truth
       values.

Compatibility

    Because of backwards compatibility, the bool type lacks many
    properties that some would like to see.  For example, arithmetic
    operations with one or two bool arguments is allowed, treating
    False as 0 and True as 1.  Also, a bool may be used as a sequence
    index.

    I don't see this as a problem, and I don't want evolve the
    language in this direction either.  I don't believe that a
    stricter interpretation of "Booleanness" makes the language any
    clearer.