I was writing code to modify the modify the year in a datetime
object, and I accidentally did the wrong thing, and it initially worked. Here is the cut-down code:
import datetime
d = datetime.datetime(1970, 1, 1, 12, 0, 0)
d2 = datetime.datetime.replace(d, 2001)
d3 = d.replace(year=2001)
match = d2 == d3
print("d2 = %s, d3 = %s, match = %s" % (d2, d3, match))
Output:
d2 = 2001-01-01 12:00:00, d3 = 2001-01-01 12:00:00, match = True
The code to create d3 is correct according to the python datetime
documentation (because datetime.datetime.replace
is a method). But somehow the code to create d2
also worked, even though I cannot find any documentation saying datetime.datetime.replace
can be used as a function. I am using Python 3.8.10
So my question is: is my code to create d2
correct, or did I just "get lucky", perhaps due to a quirk in the implementation?
The reason I ask is that, when I use the d2
code, I get a really nasty segmentation fault later on. So either the d2
code is wrong (in which case Python should have raised an exception), or it is right (in which case I should submit a bug report for the package which core dumps).