https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner

Python没有方法重载,所以像Java一样多个不同类型参数的构造器无法实现:

class Date {
  Date(int year, int month, int day);
  Date(String yyyyMMdd);
}

用@classmethod的话,python可以这样做:

class Date(object):
  def __init__(self, day=0, month=0, year=0):
    self.day = day
    self.month = month
    self.year = year
  
  @classmethod
  def fromString(cls, dateString):
    # Support the string is like "2019-02-26"
    day, month, year = map(int, date_as_string.split('-'))
    date1 = cls(day, month, year)
    return date1

而@staticmethod, 就跟Java里的static method差不多,是类方法了。比如下面这个验证日期是否正确的方法。

 @staticmethod
    def is_date_valid(date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        return day <= 31 and month <= 12 and year <= 3999