Search code examples
pythonapp-engine-ndbclass-method

Why does this gae example use a class method?


query_book In this example is defined as @classmethod, and date is an attribute.

What does cls.date mean?

If it refers to an instance attribute, then why is it in a @classmethod?

If it doesn't refer to an instance, then whose date is it referring to?

EDIT

date doesn't seem to be a class attribute, because the following code prints out two different dates for the two instances. If it was a class attribute it would print the same value.

g1 = Greeting()
g2 = Greeting()
g1.put()
g2.put()
self.response.out.write(g1.date)
self.response.out.write("<br>")
self.response.out.write(g2.date)

Solution

  • Because accessing the attribute date at class level allows to access the definition of the ndb.DateTimeProperty attribute, while accessing at instance level allows to access the value assigned to the instance.

    type(g1.date).__name__ == "datetime"
    type(Greeting.date).__name__ == "DateTimeProperty"