1. Summary
I don't find, how I can overwrite variable in function from another file.
2. Example
2.1. Configuration
I use logbook and pyfancy modules.
I have 2 files — config.py
and first.py
:
config.py
:
import logbook
import sys
from pyfancy.pyfancy import pyfancy
log = logbook.Logger("config")
logbook.StreamHandler(sys.stdout,
level=logbook.DEBUG).push_application()
def green_foreground(coloredtext):
log.debug(pyfancy().green().bold(coloredtext))
first.py
:
import logbook
from config import green_foreground
log = logbook.Logger("first")
green_foreground("Sasha Champion!")
3. Steps to reproduce
I run in console:
python first.py
4. Expected behavior
I want, that log
variable:
log = logbook.Logger("first")
— infirst.py
,log = logbook.Logger("second")
— insecond.py
and so on.
D:\PyfancyRefactoring>python first.py
[2018-01-25 16:54:25.469005] DEBUG: first: Sasha Champion!
5. Actual behavior
Value of log
variable from config.py
file.
D:\PyfancyRefactoring>python first.py
[2018-01-25 16:54:25.469005] DEBUG: config: Sasha Champion!
6. Not helped
For example, I add to config.py
:
if __name__ == 'config':
log = logbook.Logger("config")
else:
log = logbook.Logger("first")
I get actual behavior, not expected behavior.
7. Do not offer
- I know that I can define
green_foreground
function in each file, but it will be a lot of duplicate code and I think, that it a not a good practice.