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.09s whereas:
return ''.join([`num` for num in xrange(10000000)])
takes 2.43s.
Both give identical results but on is a lot slower. What is going on here?
EDIT: Oddly... repr()
gives slightly slower results then num
. 2.99s vs 2.43s. Using Python 2.6 (haven't tried 3.0 yet)