How to dynamically import and reimport a file containing definition of a global variable which may change anytime

StackOverflow https://stackoverflow.com/questions/12400467

문제

I need to have actual state of my List, which may change anytime.

mylist.py

list = {"key": "value"}

For first time import it's state is actual, but list in mylist.py may be changed. I need to reimport it each time function is called.

somecode.py

def someFunc():
    from mylist import list
    print list
도움이 되었습니까?

해결책

There is a function called reload(module)

import mylist 

def someFunc():
    mylist = reload(mylist) # this may raise syntaxerrors in wrong moments
    list = mylist.list
    print list

Does it work for you?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top