By John Paul Mueller

Variables are storage containers that hold data. When working with classes in Python, you need to consider how the data is stored and managed. A class can include both class variables and instance variables. The class variables are defined as part of the class itself, while instance variables are defined as part of methods.

Creating class variables

Class variables provide global access to data that your class manipulates in some way. In most cases, you initialize global variables using the constructor to ensure that they contain a known good value. The following steps demonstrate how class variables work.

  1. Open a Python Shell window.

    You see the familiar Python prompt.

  2. Type the following code (pressing Enter after each line and pressing Enter twice after the last line):

    class MyClass:
     Greeting = "
     def SayHello(self):
      print("Hello {0}".format(self.Greeting))

    This doesn’t include a constructor. Normally you do include a constructor to ensure that the class variable is initialized properly. However, this series of steps shows how class variables can go wrong.

  3. Type MyClass.Greeting = “Zelda” and press Enter.

    This statement sets the value of Greeting to something other than the value that you used when you created the class. Of course, anyone could make this change. The big question is whether the change will take.

  4. Type MyClass.Greeting and press Enter.

    image0.jpg

    You see that the value of Greeting has changed.

  5. Type MyInstance = MyClass() and press Enter.

    Python creates an instance of MyClass named MyInstance.

  6. Type MyInstance.SayHello() and press Enter.

    image1.jpg

    The change that you made to Greeting has carried over to the instance of the class. It’s true that the use of a class variable hasn’t really caused a problem in this example, but you can imagine what would happen in a real application if someone wanted to cause problems.

    This is just a simple example of how class variables can go wrong. The two concepts you should take away from this example are as follows:

    • Avoid class variables when you can because they’re inherently unsafe.

    • Always initialize class variables to a known good value in the constructor code.

  7. Close the Python Shell window.

Creating instance variables

Instance variables are always defined as part of a method. The input arguments to a method are considered instance variables because they exist only when the method exists. Using instance variables is usually safer than using class variables because it’s easier to maintain control over them and to ensure that the caller is providing the correct input. The following steps show an example of using instance variables.

  1. Open a Python Shell window.

    You see the familiar Python prompt.

  2. Type the following code (pressing Enter after each line and pressing Enter twice after the last line):

    class MyClass:
     def DoAdd(self, Value1=0, Value2=0):
      Sum = Value1 + Value2
      print("The sum of {0} plus {1} is {2}."
       .format(Value1, Value2, Sum))

    In this case, you have three instance variables. The input arguments, Value1 and Value2, have default values of 0, so DoAdd() can’t fail simply because the user forgot to provide values.

    Of course, the user could always supply something other than numbers, so you should provide the appropriate checks as part of your code. The third instance variable is Sum, which is equal to Value1 + Value2. The code simply adds the two numbers together and displays the result.

  3. Type MyInstance = MyClass() and press Enter.

    Python creates an instance of MyClass named MyInstance.

  4. Type MyInstance.DoAdd(1, 4) and press Enter.

    In this case, you see the sum of adding 1 and 4.

    image2.jpg

  5. Close the Python Shell window.