Programming practice: dependency of one function on another
<p>I am trying to improve my program so that it conforms to good programming 
practices. So I am looking for suggestions on whether the way I have programmed 
something is a good way of doing it.</p>

<p>I have a module called dbfunctions.py in which I have defined:</p>

<pre><code>dbparams = {
    'dbname': 'qualitysimparams',
    'tablename': 'qualityparams',
    'tablecols': ('numpeople', 'numreviews', 'prophunters', 
                  'utility_funcform', 'goods'
    )
</code></pre>

<p>and a function:</p>

<pre><code>def obtainid_ifrecord(dbname, tablename, tablecols, values):
    '''Checks if there already exists a record with given &lt;values&gt;. 
    If so, returns the id of that record, otherwise returns zero.'''
    con, c = connecttodb()
    q1 = "use {0}".format(dbname)
    c.execute(q1)
    q2p1 = "select id from {0} ".format(tablename)
    q2p2 = "where " + " = %s and ".join(tablecols) + " = %s"
    q2 = q2p1 + q2p2    
    c.execute(q2, values)
    res = c.fetchall()
    c.close()
    con.close()
    if res:
        return res[-1][0]
    else:
        return 0
</code></pre>

<p>There are other functions and variables in addition to the above two, but 
they are not relevant for this post.</p>

<p>In another file I have a function:</p>

<pre><code>def checkif_paramcomboexists(numpeople, numreviews, prophunters, 
                             utility_funcform, goods):
    '''Check in the database if the simulation has been run with the 
    specified parameters. If so return the id of that run.
    '''
    goodsjson = sjson.dumps(goods)

    # paramvalues: in same order as listed in dbf.dbparams['tablecols']
    paramvalues = (numpeople, numreviews, prophunters, 
                   utility_funcform, goodsjson)

    id = dbf.obtainid_ifrecord(dbf.dbparams['dbname'],
                               dbf.dbparams['tablename'], 
                               dbf.dbparams['tablecols'],
                               paramvalues)
    return id 
</code></pre>

<p>It seems to me that the fact that hardcoding the variable names in the 
<code>paramvalues</code> variable in function <code>checkif_paramcomboexists</code> is not a good practice.
If later I change the order of variables in dbfunctions.dbparams['tablecols'] for any 
reason, <code>checkif_paramcomboexists</code> function will fail (and can fail silently depending 
on the data types). One way to get around this is to define:</p>

<pre><code>paramvalues = [eval(x) for x in dbf.dbparams['tablecols']]
</code></pre>

<p>But I have heard that generally it is a bad practice to use <code>eval</code> (although I do not know 
why and when it is okay to use it). My questions are:</p>

<p>(i) Is it okay the way I have coded this in regards to the concern I have? I think the answer 
is 'No', but just want to check with the experts here.
(ii) Is use of <code>eval</code> as I have indicated an acceptable solution?
(iii) If answer to (ii) is 'no', what is the alternative?</p>

<p>Thank you for reading through this.</p>
