Instantiating an object

Screen Link:



I am a bit confused on instantiating objects. During the first lesson linked I had to change dates using the strptime constructor. To do that the following code was made:

import datetime as dt

date_format = "%m/%d/%y %H:%M"

for row in potus:
    start_date = row[2]
    start_date = dt.datetime.strptime(start_date, date_format)
    row[2] = start_date

I understood this code during the second start_date you type it in module.class.constructor format.

When I got confused was during the second link where when I had to make a frequency table using those dates with the following code:

visitors_per_month = {}

for row in potus:
    start_date = row[2]
    start_date = dt.datetime.strftime("%B, %Y")
    if start_date not in visitors_per_month:
        visitors_per_month[start_date] = 1
        visitors_per_month[start_date] += 1

When I tried to use the same module.class.constructor format it game me an error:

AttributeError: type object 'datetime.datetime' has no attribute 'start_date'

So I had to change it to what you currently see in the second piece of code:

start_date = start_date.strftime("%B, %Y)

Why did I have to use the module.class.constructor format in the first piece of code and not the second?

Thanks again!

1 Like

Hi @burnsdillion,

It is possible to do it in the same format:

start_date = dt.datetime.strftime("%B, %Y")

Here, you forgot to pass the start_date argument. Your code will work correctly when you do that.

start_date = dt.datetime.strftime(start_date, "%B, %Y")


1 Like