gpt4 book ai didi

python - 类设计: Last Modified

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:29 25 4
gpt4 key购买 nike

我的类(class):

class ManagementReview:
"""Class describing ManagementReview Object.
"""

# Class attributes
id = 0
Title = 'New Management Review Object'
fiscal_year = ''
region = ''
review_date = ''
date_completed = ''
prepared_by = ''

__goals = [] # List of <ManagementReviewGoals>.
__objectives = [] # List of <ManagementReviewObjetives>.
__actions = [] # List of <ManagementReviewActions>.
__deliverables = [] # List of <ManagementReviewDeliverable>.
__issues = [] # List of <ManagementReviewIssue>.

__created = ''
__created_by = ''
__modified = ''
__modified_by = ''

__modified 属性是等格式的日期时间字符串。我希望每次更新其他属性之一时,该属性自动更新为 datetime.now().isoformat() 。对于其他每个属性,我都有一个 setter ,例如:

def setObjectives(self,objectives):
mro = ManagementReviewObjective(args)
self.__objectives.append(mro)

那么,有没有比添加一行更简单的方法:

self.__modified = datetime.now().isoformat()

每个二传手?

谢谢! :)

最佳答案

要在 instance 属性被修改时更新 __modified(如您的 self.__objectives 示例),您可以覆盖 __setattr__ .

例如,您可以将此添加到您的类(class)中:

def __setattr__(self, name, value):
# set the value like usual and then update the modified attribute too
self.__dict__[name] = value
self.__dict__['__modified'] = datetime.now().isoformat()

关于python - 类设计: Last Modified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3313978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com