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

前端 未结 7 1718
逝去的感伤
逝去的感伤 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 23:00

    If you don't know the attribute name until runtime, use setattr:

    >>> import mymodule
    >>> setattr(mymodule, 'point', (1.0, 4.0))
    >>> mymodule.point
    (1.0, 4.0)
    

提交回复
热议问题