سؤال

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)
هل كانت مفيدة؟

المحلول 2

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"

نصائح أخرى

cls.date is not an instance attribute, but a class attribute.

You can also use Greeting.date. But, if the class name is changed to Goodbye, it should be changed to Goodbye.date while cls version does not.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top