scope - Global variables across multiple files in python -
i have modules.py file :
global dns_server_ip def setvnetglobalparameters(): dns_server_ip = '192.168.3.120'
and i’m importing file in abc.py file
from modules import * setvnetglobalparameters() print(dns_server_ip)
but ‘dns_server_ip’ still not accessible.
i want set global parameters through function only. appreciated! thanks..
as per question understand beginner python.
while importing modules have use module name , don't need include extension or suffix(py) , in code miss starting single quote .
here modified code: modules.py
dns_server_ip = '' def setvnetglobalparameters(): global dns_server_ip dns_server_ip = '192.168.3.120′
here abc.py
import modules modules.setvnetglobalparameters() print modules.dns_server_ip
here through global keyword telling python interpreter change or point out global variable instead of local variable , variable either global
or local
if variable both (local , global) python unboundlocalerror
exception , if did not put global keyword
global dns_server_ip
the dns_server_ip created new local variable . keyword global
intended in functions only
you can check global keyword,python modules
Comments
Post a Comment