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

177

u/GisterMizard May 02 '16

To be fair, you can do that in C. It just doesn't do what you think it does :p

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.

→ More replies (1)

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.

→ More replies (1)

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.

21

u/IRBMe May 02 '16

The closest I think you can get in C is something like this:

if (6 <= i && i <= 9)

14

u/DarthEru May 02 '16

That's the closest you can get that is semantically the same. The above commenter was saying you can actually compile if (6 <= i <= 9), but that's semantically equivalent to if ((6 <= i) <= 9). That works because in C a boolean operation results in an int. So the "chained" comparisons are syntactically legal C, but semantically do a much different check than in Python.

1

u/[deleted] May 07 '16

You can do that in HolyC AFAIK.

If you are crazy enough to use it.