Make __getstate__ work for derived classes

Expand `AffineScalarFunc.__getstate__` to work for all slots in the inheritance hierarchy. This removes the need for `Variable` to have a `__getstate__` method too.