문제

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