Well, the False ** False == True stuff did surprise me a bit, because I didn’t know that arithmetic functions worked on bools, and the Mixing numerical types part, but other than that it’s pretty obvious behaviors.
False ** False == True
Mixing numerical types
True and False mean 1 and 0, respectively. They were added to the language relatively late, after C-style integers-as-booleans had been established and was conventional. And by “mean”, I mean “are literally aliases for”, e.g.
>>> True * 10
Of course, that’s still worthy of a wat. Even more wat is that this is something they didn’t fix while breaking things for Python 3.
They aren’t literally aliases, no. The bools are different objects with different behavior from their similar integers; for example, they convert to strings differently:
>>> (str(True), str(1))
They just convert to those integers for arithmetic.
It’s true that bools are not literally aliases of int 1 and 0, but it’s very close, and they don’t convert to them. They sort of literally are them.
>>> True == 1 and True != 2 # True really is just "1"
>>> ['a', 'b', 'c'][True] # just 1..
>>> True is 1 # but still short of an alias
>>> True is True and 1 is 1 # python ints and bools are singletons
>>> True.numerator # wat
>>> isinstance(True, bool) # of course, but
>>> isinstance(True, int) # AHA!
Yes, but that’s not literally are them. :)
Also, Python ints aren’t singletons; it’s just that some of them are cached:
>>> 10000 + 10000 is 20000
@journeysquid, a lot of them I could see being crazy looking if you were new to programming. After doing this for several years, though, most of these seem pretty logical to me.
'2' * 3
It makes sense and it’s symmetrical. Much better than what many languages do.
False ** False
What else would you expect it to do?
This is simply due to floating point imprecision. I find the alternative behaviour more confusing:
scala> 9007199254740992.0 == 9007199254740993L
res6: Boolean = true
I would like a language that didn’t expose floating-point by default, but that’s probably too much to ask.
False == False in [False]
What did you expect that to do? It’s not very readable, but I don’t think it’s a wat.
1 ** -1
It’s a dynamic language, it’s supposed to be able to do that kind of thing.
Obviously correct behaviour.
Every language has some wats. But I think you can legitimately compare the frequency, and I think Python on the whole does quite well.
What is going on in the extend example?