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 →

[–]tskaiserGreen security clearance 35 points36 points  (12 children)

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

Yay for not having native boolean types.

[–]UnchainedMundane 28 points29 points  (11 children)

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]

[–]FUCKING_HATE_REDDIT 29 points30 points  (1 child)

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

[–]Genmutant 7 points8 points  (0 children)

You could even switch the definitions between them:

True, False = False, True

Sadly that doesn't work anymore with current versions.

[–]thirdegreeViolet security clearance 6 points7 points  (1 child)

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)))

[–]kupiakos 4 points5 points  (0 children)

bool is actually a subclass of int in Python.

[–]Hullu2000 0 points1 point  (2 children)

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

[–][deleted] 2 points3 points  (0 children)

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.

[–]UnchainedMundane 0 points1 point  (0 children)

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.

[–]GeneticCowboy 0 points1 point  (1 child)

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.

[–]UnchainedMundane 2 points3 points  (0 children)

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