0

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)
0

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to instanciate your class.

Like this:

daily = Daily()
daily.format_date(your_date)

I think it's what you want ?

1 Comment

your example is not for class variable but instance variable !
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.