By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Debuging-Finding out the class attributes/member variable values

100+
P: 440
Hi,

How to check the class attributes/member variable values while debuging in 'PythonWin'?.

I need to watch all the member varibales values of a class ,given the object or etc.

Thanks
PSB
May 19 '07 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hi,

How to check the class attributes/member variable values while debuging in 'PythonWin'?.

I need to watch all the member varibales values of a class ,given the object or etc.

Thanks
PSB
This is what I use to check the contents of an instance dictionary:
Expand|Select|Wrap|Line Numbers
  1. def formatDict(hdr_str, dd):
  2.     listCopy = dd.keys()
  3.     listCopy.sort()
  4.     maxLen = max([len(i) for i in listCopy])
  5.     return '%s%s%s' % (hdr_str, "\n", ''.join(["Key = %s %s Value = %s\n" % \
  6.            (i, " " * (maxLen - len(i)) + "  ", dd[i]) for i in listCopy]))
  7.  
  8. a = YourClass()
  9. print formatDict("\nObject Attributes:", a.__dict__)
HTH :)
May 19 '07 #2

100+
P: 440
This is what I use to check the contents of an instance dictionary:
Expand|Select|Wrap|Line Numbers
  1. def formatDict(hdr_str, dd):
  2.     listCopy = dd.keys()
  3.     listCopy.sort()
  4.     maxLen = max([len(i) for i in listCopy])
  5.     return '%s%s%s' % (hdr_str, "\n", ''.join(["Key = %s %s Value = %s\n" % \
  6.            (i, " " * (maxLen - len(i)) + "  ", dd[i]) for i in listCopy]))
  7.  
  8. a = YourClass()
  9. print formatDict("\nObject Attributes:", a.__dict__)
HTH :)
Thanks for the reply,

It is not a dictionary,it is a class attributes.Which I would like to watch in the Debug window of the python

-PSB
May 19 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Thanks for the reply,

It is not a dictionary,it is a class attributes.Which I would like to watch in the Debug window of the python

-PSB
I always debug with print statements. To check the status of class attributes, you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. class Demo:
  2.     a1 = 1
  3.     a2 = 2
  4.     def __init__(self, data={}):
  5.         self.data = data
  6.     def chk_att(self):
  7.         for item in ['a1', 'a2']:
  8.             print '%s = %s' % (item, eval('Demo.%s' % item))
  9.  
  10.  
  11. print '\n'.join(['%s = %s' % (item, eval('Demo.%s' % item)) for item in dir(Demo) if item in ['a1', 'a2']])
  12. print
  13. a = Demo([1,2,3,4])
  14. a.chk_att()
  15.  
  16. print
  17.  
  18. Demo.a2 = 5000
  19. print '\n'.join(['%s = %s' % (item, eval('Demo.%s' % item)) for item in dir(Demo) if item in ['a1', 'a2']])
  20. print
  21. a.chk_att()
Expand|Select|Wrap|Line Numbers
  1. >>> a1 = 1
  2. a2 = 2
  3.  
  4. a1 = 1
  5. a2 = 2
  6.  
  7. a1 = 1
  8. a2 = 5000
  9.  
  10. a1 = 1
  11. a2 = 5000
  12. >>> 
May 19 '07 #4

bartonc
Expert 5K+
P: 6,596
Hi,

How to check the class attributes/member variable values while debuging in 'PythonWin'?.

I need to watch all the member varibales values of a class ,given the object or etc.

Thanks
PSB
Since I don't use PythonWin (or any other debugger), I use the inspect module:
Expand|Select|Wrap|Line Numbers
  1. >>> import inspect
  2. >>> class aClass:
  3. ...     aValue = 1
  4. ...     bValue = 'test'
  5. ...     
  6. >>> anInstance = aClass()
  7. >>> print [(name, value) for name, value in inspect.getmembers(anInstance) if not name.startswith("_")]
  8. [('aValue', 1), ('bValue', 'test')]
  9. >>>
May 20 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.