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.
First of all: class attributes, or instance attributes? Or both? =)
Usually you just add instance attributes in
__init__, and class attributes in the class definition, often before method definitions… which should probably cover 90% of use cases.If code adds attributes on the fly, it probably (hopefully 🙂 has good reasons for doing so… leveraging dynamic features, introspection, etc. Other than that, adding attributes this way is probably less common than you think.