0

I would like to have a logger function to print out recursively all members regardless of type. For example:

magic_print(5) #5
magic_print([1,2,'abc']) # 1, 2, 'abc'
class C(object):
    def __init__(self):
        self.x = 5
c = C()
magic_print(c) #c.x = 5

Currently, if I use plain print, I get the following:

print c #<__main__.C object at 0x10c3e0fd0>

Is there such a function built in? Or do I need to write it? (Using Python 2.7)

edit - Please note, I do not ask how to print an object (someone thought I duplicated a fairly easy question) - I want to be able to "inspect" the object regardless of type. the str and repr of c (in the example) both don't show the inner members/state.

Thanks!

Tal
  • 398
  • 4
  • 19
  • 1
    If the code you shared is the actual code you are using, I do not think you are even able to create the object of `C` class because of *maximum recursion depth exceeded* error which you'll be getting. This is due to declaration of `self.y = C()` which would be creating a new object of `C` recursively – Moinuddin Quadri Feb 19 '17 at 14:37
  • @MosesKoledoye : I think OP is looking for answer using recursive `vars(class_object)` in the `magic_print` *(not the usage of `__str__` or `__repr__`)*. However this `magic_print` could be used within `__str__` or `__repr__` – Moinuddin Quadri Feb 19 '17 at 14:41
  • @MoinuddinQuadri - Thank you, you are correct. I've edited the example. – Tal Feb 19 '17 at 14:51
  • @MosesKoledoye - I've edited my question to explain what I'm looking for. I looked at the question you suggested as duplicate and it wasn't a duplicate. I hope it's clearer now. Thank you! – Tal Feb 19 '17 at 14:56

0 Answers0