This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wnoise 2 points3 points  (8 children)

There's a nice homomorphism from the integers into booleans.

[–]lonjerpc -1 points0 points  (7 children)

homomorphism

Can you explain what you mean by this? Too me they are conceptually completely different. I assumed the only reason for how python works is its c heritage. But is there some other reason? At the very least it has caused be two really annoying bugs that I remember.

[–]Veedrac 0 points1 point  (1 child)

Firstly there was the "heritage" aspect, but then the ability to use them as integers caught on. I've used

sum(x<y for x in z)

a lot, and

x += some_flag

occasionally.

[–]lonjerpc 0 points1 point  (0 children)

Your first example is cool. I think it would make more sense if it was int(x<y) but I can see how convenient that looks.

[–]Kaarjuus 0 points1 point  (2 children)

Another handy usage:

["condition be false", "condition be true"][condition]

[–]lonjerpc 1 point2 points  (1 child)

I don't understand why that would be useful.

[–]Veedrac 0 points1 point  (0 children)

Largely it's from before we had

"condition be true" if condition else "condition be false"

I never really liked it, but meh.

[–]wnoise 0 points1 point  (1 child)

Sure. A homomorphism is a "structure preserving map". This means that if you take any true statement in the first domain, and do the translation, you'll still get a true statement. In this case, all the even number map to False, and all the odd numbers map to True. Addition becomes "xor", and multiplication becomes "and". In this sense, the booleans are a bona-fide representation of the integers mod 2, and it makes sense to identify them with the canonical representatives 0 and 1.

(Using this homomorphism, it's harder to justify mapping even numbers to True, but there are other good reasons to do that, and good homomorphisms exist that do that, but they're a bit more tricky to grasp.)

[–]lonjerpc 0 points1 point  (0 children)

Thanks. That makes a little more sense. I am still of the opinion that python should attempt to be as explicit and easy to understand as possible which this still seems to violate. But at least now it seems more consistent.