Re: Unbound Local error? How?

2006-06-27 Thread Steve Holden
Hari Sekhon wrote:
> Bruno Desthuilliers wrote:
> 
>>Hari Sekhon wrote:
>>  
>>
>>>I've got some code as follows:
>>>
>>>import re
>>>re_regexname = re.compile('abc')
>>>
>>>.
>>>. various function defs
>>>.
>>>
>>>def func1():
>>>   ...
>>>   func2()
>>>   ...
>>>
>>>def func2():
>>>   if re_regexname.match('abc'):
>>>  
>>>
>>>if __name__ == '__main__':
>>>   func1()
>>>
>>>
>>>but this returns the Traceback:
>>>
>>>UnboundLocalError: local variable 're_regexname' referenced before
>>>assignment
>>>
>>>
>>
>>this is *not* the traceback. This is only the error message. The
>>traceback contains all needed informations (or at least all possible
>>information at this point) to know what happened. But you did not post
>>the traceback. Nor did you post the minimal runnable code snippet
>>producing this error.
>>
>>  
>>
>>>How?
>>>
>>>
>>
>>How could we know ?
>>
>>
>>  
>>
> 
> sorry, I know it looks like I was being stingy but the traceback was not 
> that helpful, not without seeing more a huge amount more of the code. I 
> was trying to abbreviate.
> 
> Traceback (most recent call last):
>   File "./backup.py", line 649, in ?
> backup(machine,share)
>   File "./backup.py", line 364, in backup
> backupdir(source,destination)
>   File "./backup.py", line 398, in backupdir
> (dirlist,filelist) = getdirlisting( source )
>   File "./backup.py", line 445, in getdirlisting
> if re_skip_dirs.match(x):
> UnboundLocalError: local variable 're_skip_dirs' referenced before 
> assignment
> 
> This doesn't really show that much, I figured the problem was the following:
> 
> def getdirlisting():
>  re_skip_dirs = re_skip_top_dirs   #Here's the culprit
> 
> where both these regex compiled objects were declared at the top level, 
> it seems that the assignment is trying to use the local variable 
> re_skip_top_dirs which doesn't exist, that's why I'm getting a 
> traceback, commenting out this line it runs fine.
> 
> -h
> 
> 
The error is simply that you are making an assignment *somewhere* inside 
your function body, so the compiler is treating the variable as local, 
masking the module-level global name. Consequently when you try to read 
its value you are told that the local variable has not yet been bound to 
a value.

A "global" statement inside the function body will fix the problem. You 
could also add a keyword argument (never used except to set a default 
for it in the "def" statement).

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unbound Local error? How?

2006-06-27 Thread Hari Sekhon




Bruno Desthuilliers wrote:

  Hari Sekhon wrote:
  
  
I've got some code as follows:

import re
re_regexname = re.compile('abc')

.
. various function defs
.

def func1():
   ...
   func2()
   ...

def func2():
   if re_regexname.match('abc'):
  

if __name__ == '__main__':
   func1()


but this returns the Traceback:

UnboundLocalError: local variable 're_regexname' referenced before
assignment

  
  
this is *not* the traceback. This is only the error message. The
traceback contains all needed informations (or at least all possible
information at this point) to know what happened. But you did not post
the traceback. Nor did you post the minimal runnable code snippet
producing this error.

  
  
How?

  
  
How could we know ?


  


sorry, I know it looks like I was being stingy but the traceback was
not that helpful, not without seeing more a huge amount more of the
code. I was trying to abbreviate.

Traceback (most recent call last):
  File "./backup.py", line 649, in ?
    backup(machine,share)
  File "./backup.py", line 364, in backup
    backupdir(source,destination)
  File "./backup.py", line 398, in backupdir
    (dirlist,filelist) = getdirlisting( source )
  File "./backup.py", line 445, in getdirlisting
    if re_skip_dirs.match(x):
UnboundLocalError: local variable 're_skip_dirs' referenced before
assignment

This doesn't really show that much, I figured the problem was the
following:

def getdirlisting():
     re_skip_dirs = re_skip_top_dirs   #Here's the culprit

where both these regex compiled objects were declared at the top level,
it seems that the assignment is trying to use the local variable
re_skip_top_dirs which doesn't exist, that's why I'm getting a
traceback, commenting out this line it runs fine.

-h




-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Unbound Local error? How?

2006-06-27 Thread Bruno Desthuilliers
Hari Sekhon wrote:
> I've got some code as follows:
> 
> import re
> re_regexname = re.compile('abc')
> 
> .
> . various function defs
> .
> 
> def func1():
>...
>func2()
>...
> 
> def func2():
>if re_regexname.match('abc'):
>   
> 
> if __name__ == '__main__':
>func1()
> 
> 
> but this returns the Traceback:
> 
> UnboundLocalError: local variable 're_regexname' referenced before
> assignment

this is *not* the traceback. This is only the error message. The
traceback contains all needed informations (or at least all possible
information at this point) to know what happened. But you did not post
the traceback. Nor did you post the minimal runnable code snippet
producing this error.

> 
> How?

How could we know ?


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unbound Local error? How?

2006-06-27 Thread Hari Sekhon




Diez B. Roggisch wrote:

  Hari Sekhon wrote:

  
  
import re
re_regexname = re.compile('abc')

.
. various function defs
.

def func1():
...
func2()
...

def func2():
if re_regexname.match('abc'):


if __name__ == '__main__':
func1()

  
  

The above clearly is not what you have. See the attached version of the
above that works. So - go check for a typo or something like that.

Diez
  

import re
re_regexname = re.compile('abc')

def func1():
func2()

def func2():
if re_regexname.match('abc'):
print " whohoo!"

if __name__ == '__main__':
func1()
  

you're right, it wasn't that, I was trying to locally override a regex
object as follows:

re_somepattern = re.compile('abc')
re_someotherpattern = re.compile('def')

def func():
    if somecondition:
        re_somepattern = re_someotherpattern

    
        re_somepattern.match('something')
    

not sure how to override this the way I want, it'd be quite messy if I
had to do huge if blocks to handle this inside the other code blocks
that use this re_somepattern.match()

or perhaps I'm going about this all wrong

-h



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Unbound Local error? How?

2006-06-27 Thread Diez B. Roggisch
Hari Sekhon wrote:

> import re
> re_regexname = re.compile('abc')
> 
> .
> . various function defs
> .
> 
> def func1():
> ...
> func2()
> ...
> 
> def func2():
> if re_regexname.match('abc'):
> 
> 
> if __name__ == '__main__':
> func1()


The above clearly is not what you have. See the attached version of the
above that works. So - go check for a typo or something like that.

Diezimport re
re_regexname = re.compile('abc')

def func1():
func2()

def func2():
if re_regexname.match('abc'):
print " whohoo!"

if __name__ == '__main__':
func1()
-- 
http://mail.python.org/mailman/listinfo/python-list

Unbound Local error? How?

2006-06-27 Thread Hari Sekhon
I've got some code as follows:

import re
re_regexname = re.compile('abc')

.
. various function defs
.

def func1():
...
func2()
...

def func2():
if re_regexname.match('abc'):
   

if __name__ == '__main__':
func1()


but this returns the Traceback:

UnboundLocalError: local variable 're_regexname' referenced before 
assignment


How?

It was declared in the zero level indentation near the top of the 
script! I don't understand this, isn't a function supposed to be able to 
reference stuff in the containing function or the top level?
-- 
http://mail.python.org/mailman/listinfo/python-list