The Python Way
Joel Bender
jjb5 at cornell.edu
Wed Mar 27 13:42:28 EST 2002
> lock.acquire()
> try:
> code
> code
> code
> code
> code
> code
> code
> code
> code
> code
> code
> code
> finally:
> lock.release()
>
> In any large application, it gets *tiresome* to see this, particularly
> if you add another layer of nested try/except.
In C++ it's common to have a wrapper class that acquires the lock in the
ctor and releases it in the dtor, then allocating an object of that
class on the stack local to the function. Is there a Pythonesque
version of the same design pattern? Is there any way to add the locking
functionality as a facade to a non-locking version of the same function?
I'm on the leading edge of the Python learning curve, but would this
work?
class A:
def fn(self,x):
pass
class B(A):
def fn(self,x):
lock.acquire()
try:
A.fn(self,x)
finally:
lock.release()
Or is this considered poor form?
Joel
More information about the Python-list
mailing list