Terry
Terry

Reputation: 19

I want workaround for not using global variable in the code

Below is my sample and I want a workaround for not using the global variable inside the function.

Legend = '''
<style>
table {
    font-family: Times New Roman, sans-serif;
    border-collapse: collapse;
    width: 100%;
}
td, th {
    border: 1px solid #000000;
    text-align: center;
    height: 20px;
    overflow: hidden;
    padding: 2px;
}
tr:nth-child(even) {
    background-color: #ffffff;
}
</style> '''
for i in ts:
    Legend+= '<th>' + i + '</th>'
Legend += '''</tr>'''

def get_data_legend(value,name,sym=None):
    global Legend
    L1 = []
    for i in value:
       L1.append('{:,.0f}'.format(i))
    Legend += '''<tr><th>'''+name+'''</th>'''
    for i in gt:
        if sym is not None:
             Legend+= '<td>' + sym + str(i)  + '</td>'
        else:
             Legend += '<td>' + ' ' + str(i)  + '</td>'
    Legend += '''</tr>'''

get_data_legend(update_datapoints,'Update metrics')
get_bus_metrics(update_address,'Update address')
Legend += '''</tr></table><br><br>'''

I am using the global variable inside the function that is not good to use or worst thing to use. I am looking for a workaround so that I don 't need to used global variable inside the function. I am new to the python can someone please try to help me on this.

Upvotes: 1

Views: 390

Answers (1)

timgeb
timgeb

Reputation: 78770

You have

>>> something = 'something'
>>> def bad():
...:    global something
...:    something += ' bad'
...:
>>> bad()
>>> something
>>> 'something bad'

You should have

>>> def good(something):
...:    something += ' good'
...:    return something
...:
>>> something = good('something')
>>> something
>>> 'something good'

Upvotes: 4

Related Questions