views:

78

answers:

5

All,

A class:

class foo():
    def __init__(self):
        self.avar1 = 0
        self.bvar2 = 1
        self.cvar3 = 3
    def debug_info(self):
        print "avar1:" avar1
        print "bvar2:" bvar2
        print "cvar3:" cvar3

my question, it is too complex to write the debug_info() if I got a lot of self.vars ,then I want to migrate them into a dict to print them in one go for debug and monitor purpose, however, maybe I do not want to lost the format

foo.avar1 = 0

to access the vars inside of the class, because it is easy to read and easy to use and modify. Do you have better idea instead of visit the class.dict for the debug_info output? or does someone knows a better way to make this class more simple?

Thanks!

+3  A: 
def debug_info ( self ):
    for ( key, value ) in self.__dict__.items():
        print( key, '=', value )
poke
A bit neater and more Pythonic is to use vars(self), IMO. But also see the comment below about objects using slots, for which using dir(self) and selecting from the results is necessary.
andybuckley
+1  A: 

Modules, Classes and Instances all have __dict__ so you can reference it on the instance you wish to display the debug for:

>>> f = foo()
>>> f.__dict__
{'bvar2': 1, 'cvar3': 3, 'avar1': 0}
Dave Webb
+2  A: 

Every Python object already has such a dictionary it is the self.__dict__ This prints reasonably well like and other Python dict, but you could control the format using the Data pretty printer in the Python standard library of looping through the elements in the dictionary and printing them however you want as shown below:

class foo():
    def __init__(self):
        self.avar1 = 0
        self.bvar2 = 1
        self.cvar3 = 3
    def debug_info(self):
        for k in self.__dict__:
            print k + ':' + str(self.__dict__[k])


>>> foo().debug_info()
bvar2:1
cvar3:3
avar1:0
Tendayi Mawushe
Your example output doesn't match with your code.
poke
@poke Thanks for noticing the mistake, that output was from a different version of the solution, that just printed the dict.
Tendayi Mawushe
+1  A: 

While using __dict__ will work in most cases, it won't always work - specifically for objects that have a __slots__ variable.

Instead, try the following:

vars = [var for var in dir(obj) if not callable(getattr(obj, var)) and not var.startswith('__')]
Pieter Witvoet
+1  A: 

Others have answered your question about printing. If you have a lot of variables to set in your __init__ method, you can also use __dict__ to make that a little shorter. If you have a lot of variables, this will at least keep you from having to type self all the time. I think the standard way is more readable in most cases, but your mileage may vary.

def __init__(self):
    vars = dict(avar1=0, bvar2=1, cvar3=3)
    self.__dict__.update(vars)
kindall
I wouldn't suggest to do that; it quite hides which variables are available in the object and it's squashing all the information into a small piece. Also this doesn't help with the question, as `vars` wouldn't get updated, when a object variable changes its value.
poke