Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnkit Bindal
2,830 PointsDifference between datetime.now and datetime.now() in the code ?
Sorry for asking this so late, but why are we using datetime.now instead of datetime.now() in the Entry class. If only the name of the method(datetime.now) is used, instead of calling it (datetime.now()), How is this working??
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsIn Python, functions are first class objects. That is, they can be treated like a ordinary object and be assigned to a variable.
datetime.now
refers to the function as an object and datetime.now()
executes the function and returns the current time.
From the peewee docs: A common scenario is using the current date and time. Peewee allows you to specify a function in these cases, whose return value will be used when the object is created. Note we only provide the function, we do not actually call it:
class Message(Model):
context = TextField()
timestamp = DateTimeField(default=datetime.datetime.now)
Ankit Bindal
2,830 PointsOk I got it. Thanks. :)
Ankit Bindal
2,830 PointsAnkit Bindal
2,830 PointsBut datetime.now() also returns the current time.
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsCorrect, but with the parens it will execute at compile time so every instance would have the same value. Without the parens, it will execute at instantiation time and each instance would get the time when the new instance is created.