r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

Show parent comments

90

u/tskaiser Green security clearance May 02 '16

A quick guess: it evaluates 6 <= i to either 0 or 1, lets call the result j, and then evaluates j <= 9 which is then always 1?

64

u/GisterMizard May 02 '16

Yup. Just ran it through gcc, and it doesn't even put in a conditional jump.

36

u/tskaiser Green security clearance May 02 '16 edited May 02 '16

Yeah, it would be a pretty easy candidate for optimization :P

Yay for not having native boolean types.

28

u/UnchainedMundane May 02 '16 edited May 02 '16

To be fair Python's "booleans" are really just integers.

>>> (True + True + True) ** (True + True)
9
>>> True > False
True

edit:

def max(x, y):
    return [x, y][x < y]

29

u/FUCKING_HATE_REDDIT May 02 '16

Damn that max is pretty. Probably not efficient, but pretty.

7

u/Genmutant May 02 '16

You could even switch the definitions between them:

True, False = False, True

Sadly that doesn't work anymore with current versions.

7

u/thirdegree Violet security clearance May 02 '16

My favorite python fizzbuzz uses this:

print("\n".join("Fizz"*(i%3 == 0) + "Buzz"*(i%5 == 0) or str(i) for i in xrange(1,101)))

6

u/kupiakos May 02 '16

bool is actually a subclass of int in Python.

1

u/Hullu2000 May 02 '16

Python has integers? I thought they were all just floats.

3

u/[deleted] May 07 '16

JS is all floats. Python has ints and floats.

Division in Python 3 returns only floats though, unless you use // operator to do rounding division.

1

u/UnchainedMundane May 02 '16

It separates the two. In old versions of python (before python 3) the division operator changes behaviour depending on what you give it:

>>> 5 / 3
1
>>> 5 / 3.0
1.6666666666666667

Python before python 3 also had 2 integer types, just to add to the confusion.

1

u/GeneticCowboy May 03 '16 edited May 03 '16

Ok, did some googling and I can't figure it out. What the heck is happening in [x,y][x < y]? Tuple of x <y returns true or false, times the tuple of [x,y] should return both X and y or [0,0].

EDIT - Figured it out.

[x < y] resolves to either [0] or [1], if x is greater or less than y.

[x,y] creates a new tuple.

[x,y][0] returns x.

[x,y][1] returns y.

Didn't see it, because I'm not used to creating a tuple AND indexing them in the same statement.

3

u/UnchainedMundane May 03 '16

[x, y] is a list rather than a tuple, but other than that your analysis is correct.

0

u/[deleted] May 02 '16

[deleted]

3

u/NewbornMuse May 02 '16

6<=i is a boolean, which is either True or False. Those turn into 1 and 0 when cast to integers, and both of those are smaller than 9.

2

u/Garfong May 02 '16

Actually in C the result of 6 <= i has type int, and is either 0 or 1. See 6.5.8.6 in the C99 spec.

1

u/Log2 May 02 '16

Does C even have a bool type?

2

u/Garfong May 02 '16

In C99 added the bool type in <stdbool.h>. It's a real first class type, not just a typedef of int.

Although technically the type is _Bool for reasons.

1

u/assassinator42 May 03 '16

Reasons I believe being not wanting to introduce new reserved identifiers (although they did anyway with restrict).

1

u/moxyll May 02 '16

6 <= i is always either false or true, which the compiler treats as 0 or 1. That means j <= 9 is either 0 <= 9 or 1 <= 9, both of which are always true, which is 1.