I'm playing around with list comprehensions and I came across this little snippet on another site:
return ''.join([`num` for num in xrange(loop_count)])
I spent a few minutes trying to replicate the function (by typing) before realising the `num`
bit was breaking it.
What does enclosing a statement in those characters do? From what I can see it is the equivalent of str(num). But when I timed it:
return ''.join([str(num) for num in xrange(10000000)])
It takes 4.09 seconds whereas:
return ''.join([`num` for num in xrange(10000000)])
takes 2.43 seconds.
Both give identical results, but one is a lot slower. What is going on here?
Oddly... repr()
gives slightly slower results than `num`
. 2.99 seconds vs 2.43 seconds. I am using Python 2.6 (haven't tried 3.0 yet).