I’m having a little problem to understand why python won’t cast a float to an int. The following is a code snippet.
import time
now = time.time()
print type(now)
int(now)
print type(now)
And this is the result I get, I can’t quite figure out why. Any ideas? Thanks in advance
<type 'float'>
<type 'float'>
You have to reassign
nowlike so:The
int()conversion does not operate in place – it merely returns the result of the conversion, which is why you need to reassignnowto the new value.As a general rule, functions return a new value, while methods operate in place. For the difference between functions and methods, see this question.