Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python serval variables combine into a dict?

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!

like image 359
user478514 Avatar asked Feb 26 '23 00:02

user478514


1 Answers

def debug_info ( self ):
    for ( key, value ) in self.__dict__.items():
        print( key, '=', value )
like image 187
poke Avatar answered Mar 08 '23 08:03

poke