[主程式管理篇] 如何獲取主程式資訊 - tsungjung411/python-study GitHub Wiki
關鍵字:
top-level, top-parent, main, reference, self, itself, hasattr, module
範例1
globals()
範例2
for k, v in dict(globals()).items():
print('{k}: {v}'.format(k=k, v=v))
__name__: __main__
__doc__: None
__package__: None
__loader__: <class '_frozen_importlib.BuiltinImporter'>
__spec__: None
__annotations__: {}
__builtins__: <module 'builtins' (built-in)>
範例3:動態調用頂層函數(dynamically call a top-level function)
def say_hello():
print('Hello!')
funcation_name = 'say_hello'
globals()[funcation_name]()
- 參考資料
- How to get a reference to current module's attributes in Python
Just use globals()
- How to get a reference to current module's attributes in Python