Python Attributes

Python classes are simply a bag of named attributes. An attribute being anything that follows a "." using the Python syntax. eg. Myclass.variable, MyClass.method(), MyClass.__doc__.

We classify attributes according to their function. For example:

  • An attribute that holds a value is a variable. See instence variables and class variables.
  • An attribute that is a function is called a method. See class methods and instance methods.
  • An attribute that represents a value accessed by getters/setters is call a property
class MyClass:
    value = 42                # Class Variable Attribute

    def __init__(self, x):
        self._x = x           # Instance variable Attribute

    def myMethod(self):       # Method Attribute
        return x

    @property                 # Property Attribute
    def x(self):
        return self._x;
    def x(self, value):
        self._x = value