Sharing python globals() cross files -
this questions related global-scope of python.
there have 2 files below.
filea.py
import pprint pprint.pprint(globals()) fileb.py
import filea def global_funcb(): return "global_funcb" when run python fileb.py. globals() of filea.py contains filea's content.
is possible address of global_funcb in fileb.py filea.py? far, can fileb.py.
note: purpose, there no way import fileb filea.py
[edit]
let me describe purpose clearly.
loader.py
class loader(object): def __init__(self, module): print(dir(module)) builder.py + class function
import loader class someobj(object): def somefunc(self): pass loader.loader(someobj) if passing module/class fileb build filea, it's possible fetch function of fileb.
however, if function in builder.py globally.
builder.py + global function
import loader def someglobalfunc(self): pass loader.loader(???) i have no idea how pass globals() module/class file.
if want globals of file b inside file a after importing in b , use globals in b it's redundant, can simple globals within b , use them.
if want globals , use them a it's not possible because @ time running file a there not b in it's name space obvious.
based on edit:
if going pass multiple global function loader can find functions within global namespace , pass them loader. example can objects have __call__ attribute (if classes , other objects not callable). or more general way use types.functiontype.
from typing import functiontype functions = [obj obj in globals().values() if isinstance(obj, functiontype)] the loader class:
class loader: def __init__(self, *modules): module in modules: print(dir(module)) your executable file:
import loader typing import functiontype def someglobalfunc1(self): pass def someglobalfunc2(self): pass functions = [obj obj in globals().values() if isinstance(obj, functiontype)] loader.loader(*functions)
Comments
Post a Comment