Python 如何在为类变量赋值时减少样板代码

Python 如何在为类变量赋值时减少样板代码,python,Python,是否有任何方法可以简化此代码,因为每次输入实例名来访问类变量都很烦人。我基本上是在构建Kotlin后台,我们使用和来访问类的属性 科特林例如: val notification = Notification() with(notification){ created_at = datetime.datetime.now() recipient_id = recipient //etc } 通知.py class Notification(CustomerBase, Base):

是否有任何方法可以简化此代码,因为每次输入实例名来访问类变量都很烦人。我基本上是在构建Kotlin后台,我们使用
来访问类的属性

科特林例如:

val notification  = Notification()

with(notification){
created_at = datetime.datetime.now()
recipient_id = recipient
//etc

}
通知.py

class Notification(CustomerBase, Base):
    __tablename__ = "notification"
    created_at = Column(DateTime, nullable=True, default=datetime.datetime.now())
    recipient_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    sender_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    data = Column(JSONB, nullable=True)
    message = Column(String, nullable=True)
    type = Column(String, nullable=True)
    activity_id = Column(Integer, nullable=True)
    is_read = Column(BOOLEAN, nullable=True, default=False)
    sender_info = relationship("User", foreign_keys=[sender_id])
    recipient_info = relationship("User", foreign_keys=[recipient_id])
    entity_id = Column(Integer, nullable=True)
    entity_name = Column(String, nullable=True)
将通知保存到会话

                notification = Notification()
                notification.created_at = datetime.datetime.now()
                notification.recipient_id = recipient
                notification.sender_id = activity.created_by
                notification.message = add_people_msg(user, added_user, activity)
                notification.type = NotificationTypes.PEOPLE.value
                notification.customer_id = activity.customer_id
                notification.entity_id = activity.id
                notification.customer_id = activity.customer_id
                notification.activity_id = activity.id
                notification.data = activity_data

                self.session.add(notification)
这是更多的代码行,但较少的重复

文件:

也许会有帮助。
class Notification(CustomerBase, Base):
__tablename__ = "notification"

# Define table columns
created_at = Column(DateTime, nullable=True, 
default=datetime.datetime.now())
recipient_id = Column(Integer, ForeignKey("users.id"), nullable=True)
sender_id = Column(Integer, ForeignKey("users.id"), nullable=True)
data = Column(JSONB, nullable=True)
message = Column(String, nullable=True)
type = Column(String, nullable=True)
activity_id = Column(Integer, nullable=True)
is_read = Column(BOOLEAN, nullable=True, default=False)
sender_info = relationship("User", foreign_keys=[sender_id])
recipient_info = relationship("User", foreign_keys=[recipient_id])
entity_id = Column(Integer, nullable=True)
entity_name = Column(String, nullable=True)

def __init__(*args, **kwargs):

    # Dynamically set attributes from keyword arguments
    # This is not very explicit, 
    # and therefore should be properly documented
    for k, v in kwargs:
        setattr(self, k, v)

# Set kwork arguments 
notification_args = {
    'created_at': datetime.datetime.now(),
    'recipient_id': recipient,
    'sender_id': activity.created_by,
    'message': add_people_msg(user, added_user, activity),
    'type': NotificationTypes.PEOPLE.value,
    'customer_id': activity.customer_id,
    'entity_id': activity.id,
    'customer_id': activity.customer_id,
    'activity_id': activity.id,
    'data': activity_data
}

# Add the instance to the session
self.session.add(Notification(**notification_args))