Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 available as a module attribute, but the data is only available at run time.

How can I add module attributes at run time?

like image 744
jlconlin Avatar asked Mar 18 '11 16:03

jlconlin


People also ask

How do I add an attribute to a class in Python?

Adding attributes to a Python class is very straight forward, you just use the '. ' operator after an instance of the class with whatever arbitrary name you want the attribute to be called, followed by its value.

Which function is used to get all attributes of a module?

dir() is a built-in function that also returns the list of all attributes and functions in a module.

What are module attributes?

Module attributes in Elixir serve three purposes: They serve to annotate the module, often with information to be used by the user or the VM . They work as constants. They work as a temporary module storage to be used during compilation.


Video Answer


1 Answers

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')

like image 133
jlconlin Avatar answered Oct 26 '22 03:10

jlconlin