title: Can I "detect" a slicing expression in a python class method?
body: <p>I am developing an application where I have defined a "variable" object containing data in the form of a numpy array. These variables are linked to (netcdf) data files, and I would like to dynamically load the variable values when needed instead of loading all data from the sometimes huge files at the start.</p>
  
  <p>The following snippet demonstrates the principle and works well, including access to data portions with slices. For example, you can write:</p>
  
  <pre><code>a = var()   # empty variable
  print a.values[7]   # values have been automatically "loaded"
  </code></pre>
  
  <p>or even:</p>
  
  <pre><code>a = var()
  a[7] = 0
  </code></pre>
  
  <p>However, this code still forces me to load the entire variable data at once. Netcdf (with the netCDF4 library) would allow me to directly access data slices from the file. Example:</p>
  
  <pre><code>f = netCDF4.Dataset(filename, "r")
  print f.variables["a"][7]
  </code></pre>
  
  <p>I cannot use the netcdf variable objects directly, because my application is tied to a web service which cannot remember the netcdf file handler, and also because the variable data don't always come from netcdf files, but may originate from other sources such as OGC web services.</p>
  
  <p>Is there a way to "capture" the slicing expression in the property or setter methods and use them? The idea would be to write something like:</p>
  
  <pre><code>    @property
      def values(self):
          if self._values is None:
              self._values = np.arange(10.)[slice]  # load from file ...
          return self._values
  </code></pre>
  
  <p>instead of the code below.    </p>
  
  <p>Working demo:</p>
  
  <pre><code>import numpy as np
  
  class var(object):
  
      def __init__(self, values=None, metadata=None):
          if values is None:
              self._values = None
          else:
              self._values = np.array(values)
          self.metadata = metadata  # just to demonstrate that var has mor than just values
  
      @property
      def values(self):
          if self._values is None:
              self._values = np.arange(10.)  # load from file ...
          return self._values
  
      @values.setter
      def values(self, values):
          self._values = values
  </code></pre>
  
  <p>First thought: Should I perhaps create values as a separate class and then use <code>__getitem__</code>? See <a href="https://stackoverflow.com/questions/23213057/in-python-how-do-i-create-two-index-slicing-for-my-own-matrix-class">In python, how do I create two index slicing for my own matrix class?</a> </p>