[docs] possible bug

Zachary Ware zachary.ware+pydocs at gmail.com
Wed Apr 20 15:40:52 EDT 2016


Hi Kari,

On Tue, Apr 19, 2016 at 6:19 AM, Kari Kinnunen
<kari.kinnunen at musicinfo.fi> wrote:
> Hi,
>
> I found a problem in my code.
>
> This line has no problem ...
>
> m = re.search("'(\\w+.\\w+)", str(args.rule[0]))
>
> ... until i add next line..
>
> str = ".*("
>
> Traceback (most recent call last):
>   File "....py", line 332, in <module>
>     main()
>   File "....py", line 122, in main
>     m = re.search("'(\\w+.\\w+)", str(args.rule[0]))
> UnboundLocalError: local variable 'str' referenced before assignment
>
> If I remove 'str = ..' line, then everything is again ok.

This is because you're trying to both use the str() builtin and name a
local variable "str".  Function-local variables are somewhat special
in Python: if there is an assignment to a particular name anywhere
within the function, all uses of that name must come after a local
assignment, even if the name is also available in an outer scope.
Using the name of a builtin for a purpose other than referencing that
builtin is called "shadowing", and is generally best avoided.  In this
case, just use a different name for ".*(" instead of 'str', such as
'string' or simple 's' (although an appropriately descriptive name
would be even better).

For future reference, this mailing list is actually for discussion of
Python's documentation and any inaccuracies found within the docs, not
for general bug reports or usage questions like this.  Anything like
this is perfectly welcome at python-list at python.org, though!

Regards,
-- 
Zach


More information about the docs mailing list