Popular recipes tagged "methods" but not "python" and "attributes"http://code.activestate.com/recipes/tags/methods-python-attributes/2010-09-05T17:54:50-07:00ActiveState Code RecipesObject snoop - experiment with Python special methods (Python)
2010-09-05T17:54:50-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/577383-object-snoop-experiment-with-python-special-method/
<p style="color: grey">
Python
recipe 577383
by <a href="/recipes/users/2382677/">Wai Yip Tung</a>
(<a href="/recipes/tags/metaprogramming/">metaprogramming</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/object/">object</a>).
</p>
<p>In Python, classes can define their own behavior with respect to language operators. For example, if a class defines __getitem__(), then x[i], where x is an instance of the clas, will be execute by a call to x.__getitem__(i).</p>
<p>While Python has an extensive documentation on the special methods, reading a specification may not be the best way to reveal the intricate details. <strong>object_snoop</strong> allows user to observe how Python expressions and statements are translated into special method calls. object_snoop defines most special methods. It simple print a trace and returns a fixed but sensible result. Users are invited to build complex expressions to experiment how Python special methods work.</p>