I have noticed the following in setting a class variable:
from ingest.models import WBReport
wb=WBReport()
wb.date = '2019-01-09'
The above does not set the date for the class. For example, calling this method, it prints None:
@classmethod
def load_asin(cls):
print cls.date
However, if I add another method to set that variable it does work. For example:
@classmethod
def set_date(cls, date):
cls.date=date
from ingest.models import WBReport
wb=WBReport()
wb.set_date('2019-01-09')
Why does the first method (wb.date=X)not work but the second one (wb.set_date(X)) does?
-
2Possible duplicate of What is the difference between class and instance variables?glibdud– glibdud2019年01月09日 20:10:09 +00:00Commented Jan 9, 2019 at 20:10
1 Answer 1
Instance variables and class variables exist separately. wb.date = '2019-01-09' sets an instance variable on the object wb, not the class variable WBReport.date, which is what the class method set_date sets.
The call to the class method is roughly equivalent to WBReport.date = '2019-01-09'.
2 Comments
classmethod method from the Instance? Do all instances have access to all staticmethods/classmethods?