Popular recipes tagged "functions" but not "python" and "modules"http://code.activestate.com/recipes/tags/functions-python-modules/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>
Smarter Default Arguments (Python)
2011-08-12T23:06:57-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577786-smarter-default-arguments/
<p style="color: grey">
Python
recipe 577786
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/default_arguments/">default_arguments</a>, <a href="/recipes/tags/deferred/">deferred</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>).
Revision 3.
</p>
<p>Improved handling of mutable and deferred default arguments. After the recipe you'll find an explanation of what that means.</p>
<p>Works for 2.7 with minor tweaks (getfullargspec --> getargspec).</p>
Turn a Function Into a Class (Python)
2011-10-05T18:38:43-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577822-turn-a-function-into-a-class/
<p style="color: grey">
Python
recipe 577822
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>).
</p>
<p>The only catch is that the function has to return locals() at the end. And it doesn't do the __prepare__ part of 3.x metaclasses.</p>
Batting-ball (Python)
2011-07-18T15:11:51-07:00Dominic Innocenthttp://code.activestate.com/recipes/users/4178543/http://code.activestate.com/recipes/577799-batting-ball/
<p style="color: grey">
Python
recipe 577799
by <a href="/recipes/users/4178543/">Dominic Innocent</a>
(<a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/simple/">simple</a>).
</p>
<p>Have fun batting a ball about a room & sometimes getting funny results.</p>