1

I'm relatively new to Python still, so feel free to let me know if there's something basic I'm missing.

In the interest of easy debugging, I've gotten in the habit of creating a show() procedure for every object I create. For example:

class YourMom:
    def __init__(self):
        self.name = ""
        self.age = ""
        self.children = []
    #End init()

    def show(self):
        print "Name is '%s'" % (self.name)
        print "Age is '%s'" % (self.age)
        for i in self.children:
            print "  Children: '%s'" % (i)
     #End show()
#End YourMom class

So my question is simple: Is there a programmatic way to do a show() procedure without needing to write it manually each time?

Edit: There is a similar question here: List attributes of an object But I'm looking for the VALUES in the object properties as well as the list of them.

Community
  • 1
  • 1
Locane
  • 2,886
  • 2
  • 24
  • 35
  • possible duplicate of [List attributes of an object](http://stackoverflow.com/questions/2675028/list-attributes-of-an-object) – ApproachingDarknessFish Nov 04 '14 at 19:36
  • Do you know about the [`__str__`](https://docs.python.org/2/reference/datamodel.html#object.__str__) method? That controls how the object is printed. – Cody Piersall Nov 04 '14 at 20:17
  • @CodyPiersall I'm pretty sure they intend to not have to write any object-specific method in their classes. – furkle Nov 04 '14 at 20:42

2 Answers2

1

Yes, it's a python builtin function called vars.

wim
  • 338,267
  • 99
  • 616
  • 750
  • This is what I was looking for. It's really ugly, but listing the object properties AND their values is what I wanted. – Locane Nov 04 '14 at 19:44
1

As wim says, you can use vars on any object with a __dict__ attribute. However, if you're dealing with an object that doesn't, you can use dir. Note that this returns every single method and field associated with it, even ones like __eq__ and __ne__.

If you type:

dir(YourMom())

into the interpreter, this is the result:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'children', 'name', 'show']

If you want to do anything more complicated than displaying everything, you'll probably want to check out inspect.

For example, if you only wanted to display attributes of the object that differ from any other object:

import inspect

a = YourMom()
# type is irrelevant below. comma after 'object' is critical,
# because the argument needs to be a tuple
plain_old_object = dir(type('plain', (object,), {}))
interesting_items = [item for item in inspect.getmembers(a) if item[0] not in plain_old_object]

This returns:

[('age', ''), ('children', []), ('name', ''), ('show', <bound method YourMom.show of <__main__.YourMom object at 0x02DC1B50>>)]

a could be whatever type you want, honestly - it'd be really easy to turn this into a method that just takes an object and returns a list.

furkle
  • 5,019
  • 1
  • 15
  • 24
  • 1
    The inspect function is really cool too, thank you Furkle. I think that because I'm so new and my question is so basic that vars() is probably more appropriate for future readers as the official "answer". I'm glad this is here. – Locane Nov 04 '14 at 20:04
  • 1
    @Locane Well, keep in mind that you've got a lot of noise in what you're doing. This solution actually gives you want you want, instead of 90% garbage that you're either going to have to display (which will make your logging 90% noise) or filter somehow, which is going to be difficult because you've only got strings to work with and no real way to reason about them. I agree - it's a little more complicated, but I think it's worth the effort. – furkle Nov 04 '14 at 20:06