[Newbie] Referring to a global variable inside a function

  • Thread starter =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=
  • Start date
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

Hi experts,

I've built a class for parsing a user-defined list of files and matching
lines with a user-defined list of regular expressions. It looks like this:

<code>
import re
import glob

class LineMatcher:
""" Parses a list of text files, matching their lines with the given
regular expressions and executing associated actions."""

def __init__(self):
# List of file names to parse for matching.
self.file_names = []
# Association of reg expressions to actions to execute when match.
self.regexp_action = {}

def go(self):
for file_name in self.file_names:
file = open(file_name)
for line in file:
for regexp, action in self.regexp_action.items():
match = regexp.match(line)
if match:
action(line, match.groupdict())

def add_files(self, file_pattern):
self.file_names.extend(glob.glob(file_pattern))

def add_action(self, regexp_string, action):
self.regexp_action[re.compile(regexp_string)] = action
</code>

But then, when I try to use my class using actions with "memory" it will
fail:

<code>
import LineMatcher

global count
count = 0

def line_action(line, match_dictionary):
count = count + 1

line_matcher = LineMatcher.LineMatcher()
line_matcher.add_files('*')
line_matcher.add_action(r'(?P<line>.*)', line_action)
line_matcher.go()
</code>

The error is:
<console>
Traceback (most recent call last):
File "Test.py", line 12, in ?
line_matcher.go()
File "LineMatcher.py", line 21, in go
action(line, match.groupdict())
File "Test.py", line 7, in line_action
count = count + 1
UnboundLocalError: local variable 'count' referenced before assignment
</console>

How would you do this?

Regards,
Tito
 
F

Fredrik Lundh

Ernesto said:
But then, when I try to use my class using actions with "memory" it will
fail:

<code>
import LineMatcher

global count
count = 0

def line_action(line, match_dictionary):
count = count + 1

line_matcher = LineMatcher.LineMatcher()
line_matcher.add_files('*')
line_matcher.add_action(r'(?P<line>.*)', line_action)
line_matcher.go()
</code>

The error is:
<console>
Traceback (most recent call last):
File "Test.py", line 12, in ?
line_matcher.go()
File "LineMatcher.py", line 21, in go
action(line, match.groupdict())
File "Test.py", line 7, in line_action
count = count + 1
UnboundLocalError: local variable 'count' referenced before assignment
</console>

How would you do this?

def line_action(line, match_dictionary):
global count # make it a module-global variable, not a function-local
count = count + 1

</F>
 
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

How would you do this?
def line_action(line, match_dictionary):
global count # make it a module-global variable, not a function-local
count = count + 1

</F>

OK, I had put it on the global block.

Thanks,
Ernesto
 
D

Dennis Lee Bieber

On Sun, 09 Apr 2006 12:24:51 +0200, Ernesto García García
<[email protected]> declaimed the following in
comp.lang.python:

said:
import LineMatcher

global count

This line did nothing... The global statement goes INSIDE the
function that needs to access a name /as/ a global.\
 
B

bruno at modulix

Ernesto said:
Hi experts,

I've built a class for parsing a user-defined list of files and matching
lines with a user-defined list of regular expressions. It looks like this:
(snip code)

But then, when I try to use my class using actions with "memory" it will
fail:

<code>
import LineMatcher

global count
count = 0

def line_action(line, match_dictionary):
count = count + 1 (snip)
</code>

The error is:
<console> (snip)
UnboundLocalError: local variable 'count' referenced before assignment
</console>

How would you do this?

FWIW, I would *not* use a global.

class LineAction(object):
def __init__(self):
self.count = 0

def __call__(self, line, match_dictionary):
self.count +=1

line_action = LineAction()

line_matcher = LineMatcher.LineMatcher()
line_matcher.add_files('*')
line_matcher.add_action(r'(?P<line>.*)', line_action)
line_matcher.go()

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top