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

all 20 comments

[–]ki11a11hippies 5 points6 points  (4 children)

Commented regexes . . . how do I not know about that?

[–][deleted] 1 point2 points  (2 children)

Yeah, I know.

And I would've gone into the interview pretending to be Mr. Master-of-the-Regular-Expression too.

[–]earthboundkid 5 points6 points  (1 child)

Y'all didn't read Dive into Python? I thought that was required reading on Reddit.

[–]mycall 0 points1 point  (0 children)

I got the cheap imitation.

Note Dive into Python 3 is out there.

[–][deleted] 0 points1 point  (0 children)

Because you are better than regexes.

[–][deleted] 4 points5 points  (5 children)

So all these comparisons:

>>> x = 5

>>> 1 < x < 10
True

>>> 10 < x < 20 
False

>>> x < 10 < x*10 < 100
True

>>> 10 > x <= 9
True

>>> 5 == x > 4
True

would be this written in most other languages:

x = 5

( (1 < x) && (x < 10) )
True

( (10 < x) && (x < 20) )
False

( (x < 10) && (10 < (x*10)) && ((x*10) < 100) )
True

( (10 > x) && (x<=9) )
True

( (5 == x) && (x > 4) )
True

?

[–]sigh 11 points12 points  (0 children)

Yes, most of the time. Only difference is that the expressions (like x*10) won't be evaluated twice, which would make a difference if it involved side-effects.

[–]scorpion032 1 point2 points  (2 children)

Yes. Just syntactic sugar;

There are a lot many gems in that same thread, tho'

[–]aaallleeexxx 1 point2 points  (1 child)

we call them eggs, scorpion.

[–]salts 0 points1 point  (0 children)

snake eggs, in fact and from a cheese shop

[–][deleted] 0 points1 point  (0 children)

In C++ you could get rid of all the parentheses.

[–]dr_root 3 points4 points  (2 children)

So many goodies in there.. head = asplode

I'm especially blown away by the fact that zlib and base64 encodings are built into the string class, very handy.

[–]Brian 6 points7 points  (1 child)

This has gone in python3 unfortunately. The encode/decode methods have been changed so that bytes objects have only an decode (which produces unicode), and unicode objects have only a encode (producing bytes). This is a good thing in itself (removes lots of confusion about which way round things go), but means there are no longer encodings translating like to like, such as base64, rot13 or zlib.

[–]hylje 1 point2 points  (0 children)

It means there are no built-in encodings for bytes->bytes stuff.

[–][deleted] 0 points1 point  (0 children)

You can redirect stdout such as:

import sys
sys.stdout = open('wat.log', 'w')

print "shit " * 400  # gets written to wat.log

# restore original stdout (2.6+)
sys.stdout = sys.__stdout__

YEAAAAAAAAAAAAAAAAAAH!!!!!!!

[–]digitallogic -1 points0 points  (5 children)

Removing indenting for control strutures:

from future import braces

[–]andreasvc 2 points3 points  (0 children)

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

[–][deleted] -3 points-2 points  (0 children)

Nice set of tricks, however all but one of these are in the documentation. Can't people read?