【发布时间】:2023-03-09 12:23:01
【问题描述】:
我正在研究一个需要通过__init__ 注入赋予其__dict__ 属性的类,如下所示:
class Torrent(Model):
def __init__(self, d):
super(Torrent, self).__init__('torrents')
self.__dict__ = d
并且需要确保不要更改对象的结构,因为实例最终会在 NOSQL 数据库中。我认为__slots__ 可能会有所帮助,但我需要动态定义它。
有没有办法在没有元类的情况下实现它?
【问题讨论】:
标签: python class dictionary