Ethan,
thanks a lot for your answer.

I had figured out a similar solution :)

Ciao,
Roberto


2012/11/5 Ethan Jucovy <[email protected]>

> On Thu, Nov 1, 2012 at 11:58 AM, Roberto Longobardi <[email protected]>wrote:
>
>> It works fine, unless the function itself has some code that writes to a
>> variable named like one of the parameters.
>>
>> In this case, I get an unbound local variable error when trying to use
>> the input parameter.
>>
>> I guess this is just standard Python closures behavior, but wanted to
>> check with you guys anyway:
>>
>>
>>     def save_stuff(self, t_action, t_id, t_name):
>>         @with_transaction(self.env)
>>         def do_save_stuff(db):
>>             cursor = db.cursor()
>>
>>             if t_action == 'ADD':
>>                 # "Offending" row
>>                 t_id = self.get_next_id()
>>                 cursor.execute("""
>>                     INSERT INTO stuff (id, name)
>>                         VALUES (%s,%s)
>>                 """, (t_id, t_name))
>>             else:
>>
>>                 # I get an error here for unbound local t_id variable
>>                 # unless I comment out the "t_id = self.get_next_id()"
>> row above
>>
>>                 cursor.execute("""
>>                     UPDATE stuff
>>                         SET name = %s
>>                         WHERE id = %s
>>                 """, (t_name, t_id))
>>
>
> Yup, this is standard Python behavior.  Some references:
>
>
> http://docs.python.org/2/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
>
> http://eli.thegreenplace.net/2011/05/15/understanding-unboundlocalerror-in-python/
>
> The easiest fix here would be using a local variable local_t_id, within
> the nested function, whose value defaults to the value of t_id -- like:
>
>    def save_stuff(self, t_action, t_id, t_name):
>         @with_transaction(self.env)
>          def do_save_stuff(db):
>
>             local_t_id = t_id
>
>             cursor = db.cursor()
>
>             if t_action == 'ADD':
>                 # "Offending" row
>                 local_t_id = self.get_next_id()
>                 cursor.execute("""
>                     INSERT INTO stuff (id, name)
>                         VALUES (%s,%s)
>                 """, (local_t_id, t_name))
>             else:
>                  cursor.execute("""
>                     UPDATE stuff
>                         SET name = %s
>                         WHERE id = %s
>                 """, (t_name, local_t_id))
>
> --
> You received this message because you are subscribed to the Google Groups
> "Trac Development" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/trac-dev?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/trac-dev?hl=en.

Reply via email to