How can i pass many variables to a function in a different file?
I noticed that some files only have functions (they are grouped in individual files). For example, one file to control install (that has function for new installs, update installs, etc…) , other to grant access (create new user, delete user, update user).And those functions receive a quite amount of vars , as an example:
access_tool.add_user(cur,con,env,family,iserver,version,login,
password,prefix,proxyUser,proxyPass,
proxyServer,user,pass)
So, the access_tool( the file ) and the function ( get_add_info ) receives those vars from another file, and those vars are dynamic (results from database query). How can I pass those values to a function in a different file elegantly?
Example:
We have 2 important files:
- a.properties – Which contains database info and credentials
- mainfile.py – Main file, called to execute functions for example:
- add-user ENV newuser newuserpass
The add-user calls a function from the file access_tool.py called add_user.
What mainfile.py does is (for this example of user creation ) depending on the environment sent through the input, it will do a query into the database to select the X servers to add the user and it will store the result into the vars ( to be used into a array loop ):
cur,con,env,family,iserver,version,login,password,prefix,proxyUser,proxyPass,proxyServer
So, those vars will receive from loop, 5 , 10 or more servers to add. The only variables that is static is newuser, and newuserpass (wich receives from the input )
The only problem I have is to make a decision about how I can pass (or share ) the variables to a function on another file.
Some people from work suggest that i keep all of then at the same file, and instead of having many files for each class, just have one file with all the classes.
The concept of files in Python is vague. The phrase ‘variable from a file` doesn’t mean anything. You must be aware of how these vars are instantiated and what is their scope.
Values in Python do not live in files, they live in memory, and are pointed to by variables.
For functions that take a lot of parameters, it is common to use named arguments:
Which might be the elegance you are looking for 🙂
So your code may look like this:
And your
add_userfunction may be modified like this: