problem with global var

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Jan 3 21:56:17 EST 2008


On Thu, 03 Jan 2008 11:38:48 -0300, Bruno Ferreira wrote:

> Hi,
> 
> I wrote a very simple python program to generate a sorted list of lines
> from a squid access log file.
> 
> Here is a simplified version:
> 
> ##################################
> 1  logfile = open ("squid_access.log", "r") 
> 2  topsquid = [["0", "0", "0", "0", "0", "0", "0"]]

[snip]


Others have already solved the immediate problem, but a much better 
design would be to avoid using a global variable in the first place.



def add_sorted(alist, data):
    """Add figures from alist to collated data and return data."""
    # do your processing here...
    return data



topsquid=[["0", "0", "0", "0", "0", "0", "0"]]
for line in logfile:
    linefields = logline.split()
    topsquid = add_sorted(linefields, topsquid)




-- 
Steven



More information about the Python-list mailing list