Popular recipes tagged "methods" but not "oop" and "callbacks"http://code.activestate.com/recipes/tags/methods-oop-callbacks/2017-01-14T22:35:17-08:00ActiveState Code RecipesTwo quick functions for object introspection (Python) 2017-01-14T22:35:17-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580747-two-quick-functions-for-object-introspection/ <p style="color: grey"> Python recipe 580747 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/reflection/">reflection</a>). </p> <p>This recipe shows two quick-and-clean :) utility functions for introspection of Python objects. They are meant to be used while working interactively in the reular Python shell or in the IPython shell. Both of them display attributes of any given object passed as the argument. The first function displays all attributes. The second function only displays atttributes that do not begin and end with a double underscore, so as to filter out "dunder" methods a.k.a. "special" methods - like __len__, __str__, __repr__, etc. The first function - oa(o) , where o is some object - does the same as dir(o), but is useful - in IPython - because, dir(o) output will scroll off the screen if the output is long, since it prints the attributes vertically, one per line, while oa(o) prints them horizontally, so has less chance of the output scrolling off, and the output also occupies fewer lines on the screen, so is easier to scan quickly. The second function - oar(o) - is like oa(o), but filters out attribute names that begin and end with a dunder. So it is useful in both IPython and Python.</p> <p>More information and outputs here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html</a></p> Object 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> Method Caching (Python) 2010-07-27T20:26:08-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577338-method-caching/ <p style="color: grey"> Python recipe 577338 by <a href="/recipes/users/4174445/">Danillo Souza</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Save each set of parameters with your respective return value in cache.</p>