How to Create and Reference a Class Variable in Python 


               


How to Create and Reference a Class Variable in Python



Python


In this article, we show how to create and reference a class variable in Python.

So, first, what is a class variable?

A class variable is a variable created in a class that applies to all areas of the class in Python.

It can be seen as a global variable in terms of its scope in a class. It applies to all areas of the class, including the general class (outside of instances) as well as to all instances of a class.

This is in contrast to an instance variable. An instance variable is a variable that has scope only within a particular instance of a class.

So let's dive now into some actual code of a class variable in Python and how to reference a class variable.

So below we create a class called Student.

We define a class variable within this Student class called student_age. We set this equal to 12.

We then show how to reference this class variable.

This is shown in the code below.



So what we've done in this line of code is we've created a class named Student.

In this class, we created a class variable called test_score_curve. This is a class variable because it's created in the class rather than in an instance of a class.

So this class variable applies to the entire class, including all instances created in the class.

We then create an __init__ method which defines 2 variables, name and raw_score.

name refers to the name of the student.

raw_score refers to the raw score obtained by the student before the curve is applied to the exam, which is 1.15.

We then create 2 instances of this class, Student1 and Student2.

Now let's run some example code based on this method.



So in the above code, we reference the class variable, test_score_curve

We cannot simply reference the variable with, test_score_curve.

This is because this variable belongs to a class; it is not simply a regular variable.

Therefore, we have to call the class name, followed by a dot (.), followed by the variable name.

Doing so above gives us the value of the variable, which is 1.15

We then create another variable, Student1_finalscore and set it equal to the raw score * the test curve, which gives us the final score.

So this is one way we can reference a class variable in Python.

There is another way.

Because class variable have infinite scope (or global) within a class, class variables can also accessed through instances. Because instances inherit class variables (since class variables are global in nature), we can reference a class variable through an instance of a class.

We could access the class variable through any instance of a class.

In the code below, we access the class variable through the 2 instances created in the class, Student1 and Student2.



So we access the class variable, above, not by calling the class name but by calling an instance of a class. This, again, is because instances of a class inherit all variables in the class.

Now we can get the final score of Student1 through the following code, shown below.



Another thing to note is that if you have a class variable and you define a variable within an instance of the class with the same variable name, the variable in the instance name will override the class name.

For example, if we create the following line and call the variable, we get the following.

So these are the basics of class variables in Python, how to create one and how to reference one.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...