-1

It's clear that, in Python, class-level or static variables are neither declared within constructors/methods nor are they accessed through "self." (this explains it better).

However, I am confused as to where instance variables should be declared/stored within the class.

It seems messy and potentially difficult to keep track of all of a class' instance variables if they are declared through different methods, and I wanted to learn the "Pythonic" way to do it. Is it to declare all of them in the constructor and set them to Null, only to be modified later?

Thank you!

Community
  • 1
  • 1
aralar
  • 3,022
  • 7
  • 29
  • 44
  • 1
    It *seems* like your question was [answered on Programmers.SE](http://programmers.stackexchange.com/questions/254576/is-it-a-good-practice-to-declare-instance-variables-as-none-in-a-class-in-python). – Nick T Nov 08 '14 at 01:18
  • 1
    If you expect to have instance variables it makes perfect sense to put them in __init__ method – user3885927 Nov 08 '14 at 01:21

1 Answers1

2

A couple things:

  • Class attributes are accessible through self. If python doesn't find an attribute on the instance self, it will begin searching on type(self) etc.
  • If you think it would improve clarity to set the most common instance attributes to None, you should do that in __init__.
David Sanders
  • 4,069
  • 1
  • 24
  • 38