Inactive Tutor answered 10/09/19
Tutor
New to Wyzant
Yes! Static variables are possible in Python, as are static methods. The Python nomenclature is somewhat different from other languages such as Java and C++, and is not entirely internally consistent.
- In Python, a static variable is the same as a class variable. These are variables that are stored in the class, not in an instance of the class, and cannot be changed.
- In Python, a static method (function) is not the same as a class method (function).
- A static method cannot access or modify any information from the class or any instance of the class; it takes in arguments, processes them as the code specifies, and returns a value.
- A class method can access the state of the class and modify information in the class, which would affect all instances of the class.
Let's say we have a class representing a Person.
- All Persons are of the same species, which is the same for all Persons and can't change, so we want to represent that information as a static (class) variable.
- Persons also have a name and an age, which are different for each Person and can change, so we represent those as instance variables.
- Persons should be able to tell you their name and age, so we have instance methods to return these values.
- Let's also say we want the Person to be able to do simple math regardless of any of his attributes; specifically, he should be able to add two numbers. We will do this with a static method.
- Finally, let's say we want the Person to be able to "hear" (receive as an argument) a species name and tell us whether he is of that species. For this we would need a class method, since we are taking in an argument and comparing it to a class variable. We will represent the output as a Boolean value.
Here is what the code would look like:
class Person:
""" This is a static or class variable, defined outside the constructor.
Note that we do not refer to this variable the current instance using
`self` (that is, `self.species`) as we would if this were an instance
variable, but rather, simply `species`. If we want to refer to this
class variable from within a class method, we will need to refer to the
class as `cls` (that is, `cls.species`).
"""
species = "human"
def __init__(self, name, age):
""" Inside this constructor we have instance variables.
Note that to access the instance variables, we must refer to
the current instance, `self`.
"""
error1 = "Invalid arg: `name` not string"
error2 = "Invalid arg: `age` not numeric"
assert isinstance(name, str), error1
assert isinstance(age, (int, float)), error2
self.name = name
self.age = age
def get_name(self):
""" An instance method to return the instance variable `name`. Instance
methods can access all information in an instance of a class. Note
also the explicit reference to the current instance, `self`, used to
access the instance variable `name`.
"""
return self.name
def get_age(self):
""" An instance method to return the instance variable `age`. Instance
methods can access all information in an instance of a class. Note
also the explicit reference to the current instance, `self`, used to
access the instance variable `age`.
"""
return self.age
@staticmethod
def add(num1, num2):
""" This is a static method. It cannot access any information from
the class, nor of any instance of the class. Note especially
the `@staticmethod` decorator. This tells Python that the
method is a static method.
"""
error1 = "Invalid arg: `num1` not numeric"
error2 = "Invalid arg: `num2` not numeric"
assert isinstance(num1, (int, float)), error1
assert isinstance(num2, (int, float)), error2
return num1 + num2
@classmethod
def is_same_species(cls, other_species):
""" This is a class method. It can access any information from the
state of the class, but not from any instance of the class.
Note especially the `@classmethod` decorator. This tells Python
that the method is a class method. Note also the explicit reference
to the class, `cls`, used to access the class variable `species`.
"""
error = "Invalid arg: `other_species` not string"
assert isinstance(other_species, str), error
return cls.species == other_species