Skip to content

Attributes and Methods

Attributes store object state; methods define behavior. Use instance attributes for per-object state and class attributes for shared data.

class Counter:
    step = 1  # class attribute

    def __init__(self, start=0):
        self.value = start  # instance attribute

    def inc(self):
        self.value += self.step

c = Counter()
c.inc()
print(c.value)
classdef Counter
  properties
    value = 0;
  end
  properties (Constant)
    step = 1;
  end
  methods
    function obj = inc(obj)
      obj.value = obj.value + obj.step;
    end
  end
end

Gotchas

  • Access class attributes via the class name or self.__class__ to avoid accidental shadowing.
  • In Matlab, class properties have attributes (like Constant) that change their behavior.
  • Avoid large mutable class-level defaults unless intentionally shared.