title: Python - creating a class that locks objects using threading.lock()
body: <p>Trying to create a class that can lock objects using threading.lock:</p>
  
  <pre><code>class ObjectLock(object):
      def __init__(self):
          self._lock = threading.Lock()
          self._value = False
      def __str__(self):
          self.acquire() 
          try: 
              if(self._value):
                  return self._value
              else:
                  return self._value
          finally: 
              self.release() 
      def __enter__(self):
          self.acquire()
      def __exit__(self, type, value, traceback):
          self.release()
      def acquire(self):   
          self._lock.acquire()
      def release(self):
          self._lock.release() 
      def locked(self):
          return self._lock.locked()
  
  lock = ObjectLock()
  
  print(lock)
  with lock:
      print (lock)
  </code></pre>
  
  <p>The issue is that I'm trying to implement "with". If a method is called within a "with" block (such as <strong>str</strong>, the lock is re-aquired...which is bad. Do I have to create a seperate set of methods to call when I know I am working within a lock?</p>