When coding in Python, the common way to get the current time is time.time()
or datetime.datetime.now()
. But I never doubted their result until this code:
import time
class A(object):
def __get__(self, instance, owner):
print '__get__ is run'
return time.time()
def __set__(self, instance, value):
print('let me set, but i will not', value)
class C(object):
a = A()
c1 = C()
c2 = C()
print(c1.a)
c1.a = time.time()
print(c1.a)
print('-' * 20)
print(c2.a)
The result of this code is:
__get__ is run
1512115138.19
('let me set, but i will not', 1512115138.188)
__get__ is run
1512115138.19
--------------------
__get__ is run
1512115138.19
[Finished in 0.2s]
What happened on earth? Why do these three time.time()
calls get the same result?
The same result is for datetime.datetime.now()
. And the same result in python 2 and python 3.
I can't find some python technique to explain it.