In the process of using Python-written apps, you sometimes encounter situations where multiple files pass the same global variable. In this paper, the following solutions are provided for everyone's reference.
File 1:globalvar.py
#!/usr/bin/env python2.7 class Globalvar: Db_handle = none mq_client = none def set_db_handle (db): Globalvar.db_handle = db def get_db_handle (): return globalvar.db_handle def set_mq_client (mq_cli): Globalvar.mq_client = Mq_cli def get_mq_client ():
File 2:set.py
Import Globalvar as Globalvar def set (): globalvar.set_mq_client (10)
File 3:get.py
#!/usr/bin/env python2.7 Import Globalvar as Globalvar def get ():
File 4:main.py
Where globalvar.py defines two global variables, it is assigned in the SET function in set.py, and the Get function in the get.py file is evaluated and printed. The main.py function acts as an application entry, calling set and get.
This allows you to see a complete application in which global variables are used across files.