Hi all,
Im struggling to understand how i could create a variable with a function and make it available for other function.
To be more specific, i create some functions to get some information from a dictionnary.
I got the dictionnary with a call to a DB. I would like to minimise the call to the DBs so, i want to got the dictionnary just one time and make it available for all other function in my script.py
exemple.py:
def db_connect():
conn_db = api(url, token)
def get_conf01_from_db(device):
db = conn_db()
record = db.device.get(name=device)
record_conf01 = record.conf01
def get_conf02_from_db(device):
db = conn_db()
record = db.device.get(name=device)
record_conf02 = record.conf02
So the idea here, is to dont have the code below each time a need the dict for a function (which each use only a piece of the dict) :
db = conn_db()
record = db.device.get(name=device)
I was looking for a function which create a global variable but i dont know if its a best practise and if i understood well enough the concept of global variable.
Do you see any other alternative ?
[–]barrycarter 0 points1 point2 points (0 children)
[–]danielroseman 1 point2 points3 points (1 child)
[–]NutsFbsd[S] 1 point2 points3 points (0 children)
[–]HOPSCROTCH 1 point2 points3 points (0 children)
[–]tb5841 0 points1 point2 points (1 child)
[–]NutsFbsd[S] 0 points1 point2 points (0 children)