Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good way to keep track of class instance variables in Python?

I'm a C++ programmer just starting to learn Python. I'd like to know how you keep track of instance variables in large Python classes. I'm used to having a .h file that gives me a neat list (complete with comments) of all the class' members. But since Python allows you to add new instance variables on the fly, how do you keep track of them all?

I'm picturing a scenario where I mistakenly add a new instance variable when I already had one - but it was 1000 lines away from where I was working. Are there standard practices for avoiding this?

Edit: It appears I created some confusion with the term "member variable." I really mean instance variable, and I've edited my question accordingly.

like image 769
Michael Kristofik Avatar asked Jan 30 '09 18:01

Michael Kristofik


1 Answers

pylint can statically detect attributes that aren't detected in __init__, along with many other potential bugs.

I'd also recommend writing unit tests and running your code often to detect these types of "whoopsie" programming mistakes.

like image 75
Ryan Avatar answered Nov 14 '22 22:11

Ryan