Python Using Databases in Python Our Diary App Doing Data Entry

Anthony Grodowski
Anthony Grodowski
4,736 Points

Difference beetween ```.now()``` and ```.now```

I think I don't have a correct understanding on the difference between datetime.datetime.now and datetime.datetime.now(). Can someone clear that up for me?

1 Answer

Steven Parker
Steven Parker
171,958 Points

When you call a method, you put parentheses after the method name. WIthout them, you are referencing the method without invoking it.

So the first example would return the resut of calling the method (which would be the current date and time), but the second one is a reference to the method itself.

Anthony Grodowski
Anthony Grodowski
4,736 Points

Thanks Steven! So what's the point of putting that in here:

class Entry(Model):
    # content
    content = TextField() # it allows us to put as much text as we want
    # timestamp
    timestamp = DateTimeField(default = datetime.datetime.now)

if it's just referencing to the method and it doesn't return anything?

Steven Parker
Steven Parker
171,958 Points

It returns a reference to the method, which is assigned as the "default". So when a record is added without an explicit timestamp value, then the method will be called to fill the timestamp with the current date/time.