python leading and trailing underscores in user defined functions
<p>How can i used the rt function, as i understand leading &amp; trailing underscores <code>__and__()</code> is available for native python objects or you wan't to customize behavior in specific situations. how can the user take advantages of it . For ex: in the below code can i use this function at all,</p>

<pre><code>class A(object):
  def __rt__(self,r):
      return "Yes special functions"


a=A()
print dir(a)
print a.rt('1') # AttributeError: 'A' object has no attribute 'rt'
</code></pre>

<p>But</p>

<pre><code>class Room(object):



  def __init__(self):
     self.people = []

  def add(self, person):
     self.people.append(person)

   def __len__(self):
     return len(self.people)



room = Room()
room.add("Igor")
print len(room) #prints 1
</code></pre>

<p>Python doesn't translate one name into another. Specific operations will <em>under the covers</em> call a <code>__special_method__</code> if it has been defined. For example, the <code>__and__</code> method is called by Python to hook into the <code>&amp;</code> operator, because the Python interpreter <em>explicitly looks for that method</em> and documented how it should be used.</p>

<p>In other words, calling <code>object.rt()</code> is not translated to <code>object.__rt__()</code> anywhere, not automatically.</p>

<p>Note that Python <em>reserves</em> such names; future versions of Python may use that name for a specific purpose and then your existing code using a <code>__special_method__</code> name for your own purposes would break.</p>

<p>From the <a href="https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers" rel="nofollow"><em>Reserved classes of identifiers</em> section</a>:</p>

<blockquote>
  <p><code>__*__</code><br>
  System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the <a href="https://docs.python.org/3/reference/datamodel.html#specialnames" rel="nofollow">Special method names</a> section and elsewhere. More will likely be defined in future versions of Python. Any use of <code>__*__</code> names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.</p>
</blockquote>

<p>You can ignore that advice of course. In that case, you'll have to write code that actually <em>calls your method</em>:</p>

<pre><code>class SomeBaseClass:
    def rt(self):
        """Call the __rt__ special method"""
        try:
            return self.__rt__()
        except AttributeError:
            raise TypeError("The object doesn't support this operation")
</code></pre>

<p>and subclass from <code>SomeBaseClass</code>.</p>

<p>Again, Python won't automatically call your new methods. You still need to actually write such code.</p>

python