Persistent variable in Python function

glarue

I've got this script written which does some involved parsing while running through a large file. For every line in the file (after some heavy manipulation), I need to add a check to see whether it meets certain criteria and if it does, include it in a list for additional processing later.

The function doing the parsing is already a bit cluttered, and I'm wondering whether it's possible to shunt the line-checking and list manipulation to another function to make things easier to modify later on. My inclination is to use a global variable which gets modified by the function, but I know that's usually poor form. Until now I have never made use of classes, but I vaguely recall there being some advantage to them regarding persistent local variables.

One version of this part of the script could be:

matchingLines = []
def lineProcess(line):
    global matchingLines
    if line.startswith(criteria):
        matchingLines.append(line)
for line in myFile:
    # lots of other stuff
    lineProcess(line)

Obviously in this simple example it's not much of a pain to just do the checking in the main function and not bother with any additional functions. But in principle, I'm wondering what a better general way of doing this sort of thing is without using external variables.

EDIT: Part of the reason I find the separate function attractive is because once I've collected the list of lines, I am going to use them to manipulate another external file and it would be convenient to have the whole operate wrapped up in a contained module. However, I recognize that this might be premature optimization.

Tom Dalton

Something like the following might be considered more pythonic:

def is_valid_line(line):
    """Return True if the line is valid, else False."""
    return line.startswith(criteria)

valid_lines = [l for l in myFile if is_valid_line(l)]

Incidentally it would be even better practice to use a generator expression rather than a list e.g.

valid_lines = (l for l in myFile if is_valid_line(l))

That way the file reading and line-validation will only actually happen when something tries to iterate over valid_lines, and not before. E.g. In the following case:

valid_lines = [l for l in myFile if is_valid_line(l)]
for line in valid lines:
    stuff_that_can_raise_exception(line)

In this case, you have read and validated the entire (huge) file and have the full list of validated lines, and then the first line causes an error, the time spent validating the whole file is wasted. If you use a generator expression (the (x for x in y)) version instead of a list comprehension (the [x for x in y]) then when the error happens you haven't actually validated the file yet (only the first line). I only mention it because I am terrible for not doing this more often myself, and it can yield big performance gains (in CPU and memory) in a lot of cases.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Persistent variable in Python function

From Dev

How to make a persistent value from variable in a lambda function

From Dev

Persistent variable in an SQL database?

From Dev

Nodejs Persistent Variable

From Dev

Persistent variable in view

From Dev

Python: doubling variable in a function

From Dev

Python - Store function in variable

From Dev

Variable as function parameter in Python

From Dev

python variable not updating in function

From Dev

Python function with a unassigned variable

From Dev

Storing a function as a variable in Python

From Dev

Python variable redefinition in a function

From Dev

How to make variable values persistent between function calls in c / c++

From Dev

Persistent session variable in Polymer 1.0

From Dev

How to set a global persistent variable?

From Dev

Persistent global variable in linux shell

From Dev

Creating persistent variable in mysql(5.7)

From Dev

Shared Persistent Storage in Python

From Dev

Implementing Python persistent properties

From Dev

passing a variable with colon in function python

From Dev

Is an undeclared variable a reference to a function in Python?

From Dev

Call a function through a variable in Python

From Dev

Assigning a variable directly to a function in Python

From Dev

static variable in a function with Python Decorator

From Dev

Python class variable with function call?

From Dev

Python: function and variable with same name

From Dev

Python: function and variable with the same name

From Dev

Python - Static Variable inside Function

From Dev

Python modifying the local variable in function