How can I add attributes to a module at run time?

前端 未结 7 1761
逝去的感伤
逝去的感伤 2021-01-03 22:04

I have a need to add module attributes at run time. For example, when a module is loaded, it reads the file where the data is contained. I would like that data to be avail

7条回答
  •  温柔的废话
    2021-01-03 22:52

    Thanks @Dharmesh. That was what I needed. There is only one change that needs to be made. The module won't be importing itself so to get the module object I can do:

    setattr(sys.modules[__name__], 'attr1', 'attr1')

提交回复
热议问题