Interesting Problem

James Matthews nytrokiss at gmail.com
Wed Jan 20 11:49:19 EST 2010


Also as a side point... It's best to anonymize the code as best you can so
people cannot attach your code to your site (IP theft and security risk)

On Wed, Jan 20, 2010 at 8:38 AM, MRAB <python at mrabarnett.plus.com> wrote:

> Victor Subervi wrote:
>
>> Hi;
>> I think I finally have an interesting problem for y'all. I need to import
>> a script from a lower dir, forcing me to change dirs:
>>
>> cwd = os.getcwd()
>> os.chdir('%s/..' % cwd)
>> sys.path.append(os.getcwd())
>> from templateFrame import top, bottom
>> os.chdir(cwd)
>>
>>  There's no need to actually change current directory. Just do this:
>
> sys.path.append(os.path.dirname(os.getcwd()))
>
> from templateFrame import top, bottom
>
>  Because I've found I must do my form evaluations *before* changing dir as
>> above, I am forced to call these values as globals:
>>
>> form = cgi.FieldStorage()
>> store = form.getfirst('store')
>> cat = form.getfirst('cat')
>> id = form.getfirst('id')
>> pkg = form.getfirst('pkg')
>> patientID = form.getfirst('patientID')
>>
>> Now, apparently because of python's problem with globals, when I call "id"
>> as follows:
>>
>> cursor.execute('select ProductID from productsPackages where PackageID=%s'
>> % id)
>>
>> I get the following error:
>>
>> /var/www/html/angrynates.com/cart/Store_frame2.py <
>> http://angrynates.com/cart/Store_frame2.py>
>>
>>  135   cursor.close()
>>  136   bottom()
>>  137
>>  138 Store_frame2()
>>  139
>> Store_frame2 = <function Store_frame2>
>>  /var/www/html/angrynates.com/cart/Store_frame2.py <
>> http://angrynates.com/cart/Store_frame2.py> in Store_frame2()
>>
>>  119     printAProduct()
>>  120   else:
>>  121     cursor.execute('select ProductID from productsPackages where
>> PackageID=%s' % id)
>>  122     for id in [itm[0] for itm in cursor]:
>>  123       printAProduct(id)
>> global cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound
>> method Cursor.execute of <MySQLdb.cursors.Cursor object>>, global id = '1'
>>
>> UnboundLocalError: local variable 'id' referenced before assignment
>>      args = ("local variable 'id' referenced before assignment",)
>>
>> Full code below. Please advise.
>>
>>  Line 121 refers to 'id', but line 122 assigns to 'id' (the 'for' loop).
>
> In a function, if you assign to a variable then that variable defaults
> to being local. You're trying to use the local variable before assigning
> a value to it. Having a local variable called 'id' means that you can't
> refer to the global variable of the same name.
>
> You're also assigning to 'i' on line 113, but not referring to it
> elsewhere in the function.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.astorandblack.com

--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100120/7ef654c2/attachment-0001.html>


More information about the Python-list mailing list