What do I pass as the first parameter “object” to the function setattr(object, name, value), to set variables on the current module?
For example:
setattr(object, "SOME_CONSTANT", 42);
giving the same effect as:
SOME_CONSTANT = 42
within the module containing these lines (with the correct object).
I’m generate several values at the module level dynamically, and as I can’t define __getattr__ at the module level, this is my fallback.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
import sys thismodule = sys.modules[__name__] setattr(thismodule, name, value)
or, without using setattr (which breaks the letter of the question but satisfies the same practical purposes;-):
globals()[name] = value
Note: at module scope, the latter is equivalent to:
vars()[name] = value
which is a bit more concise, but doesn’t work from within a function (vars() gives the variables of the scope it’s called at: the module’s variables when called at global scope, and then it’s OK to use it R/W, but the function’s variables when called in a function, and then it must be treated as R/O — the Python online docs can be a bit confusing about this specific distinction).
Method 2
In Python 3.7, you will be able to use __getattr__ at the module level (related answer).
Per PEP 562:
def __getattr__(name):
if name == "SOME_CONSTANT":
return 42
raise AttributeError(f"module {__name__} has no attribute {name}")
Method 3
If you must set module scoped variables from within the module, what’s wrong with global?
# my_module.py
def define_module_scoped_variables():
global a, b, c
a, b, c = 'a', ['b'], 3
thus:
>>> import my_module >>> my_module.define_module_scoped_variables() >>> a NameError: name 'a' is not defined >>> my_module.a 'a' >>> my_module.b ['b']
Method 4
- You wouldn’t. You would do
globals()["SOME_CONSTANT"] = 42 - You wouldn’t. You would store dynamically-generated content somewhere other than a module.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0