Eventually I will be using python datetime
methods such as day
, month
and year.
I can easily assign them manually:
import datetime as dt
year = dt.datetime.today().year
month = dt.datetime.today().month
day = dt.datetime.today().day
But as I need to further manipulate them (e.g. transform to str
, I guess it is better to do variable assignment inside a loop)
What I have tried so far, and the error messages are displayed below (I have tried one x
variable assignment at the time i.e. when I tried one variable assignment, the other attempts were commented)
import datetime as dt
today = dt.datetime.today()
for i in 'year month day'.split():
x = today.i # AttributeError: 'datetime.datetime' object has no attribute 'i'
x = f"{today}.{i}" # does not return an error, but returns '2023-01-04 12:06:45.614862.day'
x = today.eval(i) # AttributeError: 'datetime.datetime' object has no attribute 'eval'
x = eval(today.i) # AttributeError: 'datetime.datetime' object has no attribute 'i'
So in a nutshell, what I want in my loop is that python transforms the i
strings (year, month and day) and use that as a method for today()