0
下面是我的代碼不能從一個函數內部訪問全局變量在python
global PostgresDatabaseNameSchema
global RedShiftSchemaName
PostgresDatabaseNameSchema = None
RedShiftSchemaName = None
def check_assign_global_values():
if not PostgresDatabaseNameSchema:
PostgresDatabaseNameSchema = "Superman"
if not RedShiftSchemaName:
RedShiftSchemaName = "Ironman"
check_assign_global_values()
但我得到一個錯誤說
Traceback (most recent call last):
File "example.py", line 13, in <module>
check_assign_global_values()
File "example.py", line 8, in check_assign_global_values
if not PostgresDatabaseNameSchema:
UnboundLocalError: local variable 'PostgresDatabaseNameSchema' referenced before assignment
所以我們不能訪問或設置全局函數內部的變量?
[在創建它們的函數中使用全局變量]的可能重複(http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-該一個 - 即創建,它們) – putonspectacles