在类变量python中使用类方法
如何对类变量使用format_date函数在类变量python中使用类方法,python,Python,如何对类变量使用format_date函数 class DailyAggregator(Aggregator): time_dirty_data = self.format_date(datetime.datetime.now()) @staticmethod def format_date(date): return date.replace(hour=0, minute=0, second=0, microsecond=0) 你需要实例化你
class DailyAggregator(Aggregator):
time_dirty_data = self.format_date(datetime.datetime.now())
@staticmethod
def format_date(date):
return date.replace(hour=0, minute=0, second=0, microsecond=0)
你需要实例化你的类 像这样:
daily = Daily()
daily.format_date(your_date)
我想这就是你想要的吗?你需要使用
@classmethod
装饰器,并参考每日使用,比如
class Daily(Aggregator):
time_dirty_data = None
def __init__(self):
Daily.time_dirty_data = Daily.format_date(datetime.datetime.now())
@classmethod
def format_date(cls, date):
return date.replace(hour=0, minute=0, second=0, microsecond=0)
print(Daily.time_dirty_data)
d = Daily()
print(Daily.time_dirty_data)
您可以使用静态方法,并按如下方式使用:
@staticmethod
def format_date(date):
return date.replace(hour=0, minute=0, second=0, microsecond=0)
time_dirty_data = format_date.__func__(datetime.datetime.now())
您的示例不是针对类变量,而是针对实例变量!