What does self[] mean in a method?
<p>When reading a python program, I once find a function uses <code>self</code> in the following way. </p>

<pre><code>class Auto(object):
   _biases_str = "biases{0}"

   def _b(self, n, suffix=""):
     name_b_out = self._biases_str.format(i + 1) + "_out"
     return `self[self._biases_str.format(n) + suffix]`
</code></pre>

<p>The line of <code>name_b_out = self._biases_str.format(i + 1) + "_out"</code>  looks usual to me, i.e., we always <code>self.</code> to define something. But I am not very clear about the usage of <code>self[self._biases_str.format(n) + suffix]</code>. In specifc, what does <code>self[]</code> mean here, or what does it do?</p>

<p>It doesn't mean anything other than what <code>variable[something]</code> would mean. In Python <code>variable[something]</code> in an expression will just call <code>variable.__getitem__(something)</code>, and <code>self</code> is the name conventionally used for the current object, or the receiver, in Python, so <code>self[self._biases_str.format(n) + suffix]</code> would mean pretty much the same as <code>self.__getitem__(self._biases_str.format(n) + suffix)</code></p>

<p>I guess there is a method <code>__getitem__</code> defined for that class as well.</p>

python python-2.7 python-3.x