Python: how to count the access of an instance variable
<p>I have a python class as below.</p>

<pre><code>class A(object):
   def __init__(self, logger):
       self.b = B()
       self.logger = logger
   def meth1(self):
       self.b.mymethod1()
   def meth2(self):
       self.meth1()
       self.b.mymethod2()
   .........
class B(object):
   ---------
</code></pre>

<p>How can I count how many time I accessed self.b variable on the invocation of meth2() or any method of class A. Is there any way, I can log the usage of self.b variable?</p>

<p>You can use property, somtehing like:</p>

<pre><code>class A(object):
  def __init__(self, logger):
      self._b = B()
      self._count = 0
      self.logger = logger

  @property
  def b(self):
     self._count += 1
     return self._b
  ...
  ...
</code></pre>

python class