Popular recipes tagged "objects"http://code.activestate.com/recipes/tags/objects/popular/2017-04-20T23:34:50-07:00ActiveState Code RecipesImplementing class-based callbacks in Python (Python)
2017-04-20T23:34:50-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580788-implementing-class-based-callbacks-in-python/
<p style="color: grey">
Python
recipe 580788
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/callbacks/">callbacks</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is a follow-on to this recently posted recipe:</p>
<p>Implementing function-based callbacks in Python:
<a href="https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351</a></p>
<p>This new recipe shows how to create and use callbacks in Python, using classes with methods, instead of plain functions, as was done in the recipe linked above. All other points such as reasons and benefits for using callbacks, are more or less the same as mentioned in the previous recipe, except that class instances can carry state around, so to that extent, the two approaches are different.</p>
<p><a href="https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html</a></p>
Two 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>
Humanize decorator (Python)
2013-07-31T16:04:13-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/578619-humanize-decorator/
<p style="color: grey">
Python
recipe 578619
by <a href="/recipes/users/2520014/">tomer filiba</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/pretty/">pretty</a>, <a href="/recipes/tags/print/">print</a>).
</p>
<p>When you need to inspect Python objects in a human-readable way, you're usually required to implement a custom <code>__str__</code> or <code>__repr__</code> which are just boilerplate (e.g., <code>return "Foo(%r, %r, %r)" % (self.bar, self.spam, self.eggs)</code>. You may implement <code>__str__</code> and <code>__repr__</code> by a base-class, but it's hard to call it <em>inheritance</em> and moreover, you may wish to remove it when you're done debugging.</p>
<p>This simple (yet complete) recipe is a class decorator that injects <code>__str__</code> and <code>__repr__</code> into the class being printed. It handles nesting and even cycle detection, allowing you to just plug it into existing classes to get them pretty-printed and perhaps remove it later.</p>