iuuujkl
iuuujkl

Reputation: 2384

use class methods in class variable python

How can I use my format_date function for my class variable?

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)

Upvotes: 0

Views: 47

Answers (3)

Maheen Saleh
Maheen Saleh

Reputation: 183

You can the method static and use like this:

@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())

Upvotes: 0

general03
general03

Reputation: 863

You need to use @classmethod decorator and use with reference Daily, like

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)

Upvotes: 1

LeMorse
LeMorse

Reputation: 132

You need to instanciate your class.

Like this:

daily = Daily()
daily.format_date(your_date)

I think it's what you want ?

Upvotes: 0

Related Questions