Default scope of variables
Ethan Furman
ethan at stoneleaf.us
Tue Jul 9 15:19:14 EDT 2013
On 07/09/2013 11:41 AM, Ian Kelly wrote:
> On Tue, Jul 9, 2013 at 11:23 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 07/09/2013 09:44 AM, Ian Kelly wrote:
>>>
>>> On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>>>>
>>>> You could also do it like this:
>>>>
>>>> def updating(self):
>>>> self.transaction_active = True
>>>> return self
>>>
>>>
>>> Yes, that would be simpler. I was all set to point out why this
>>> doesn't work, and then I noticed that the location of the
>>> "transaction_active" attribute is not consistent in the original code.
>>> The DbSession class places it on self, and then the example usage
>>> places it on the connection object
>>
>>
>> It looks like DbSession has a conn object, and in the example he has
>> DbSession() named as conn -- ironic, considering this is a variable scoping
>> thread. ;)
>
> The object returned by __enter__ is the conn object, not the
> DbSession, so naming it "conn" is correct.
Huh. I didn't realize a different object could be returned by __enter__ without affecting which object's __exit__ gets
called. Thanks for the lesson! :)
--
~Ethan~
More information about the Python-list
mailing list