classmethods, class variables and subclassing
a.jaffe at imperial.ac.uk
Fri Oct 21 16:00:50 CEST 2005
Steve Holden wrote:
> Andrew Jaffe wrote:
>> Steve Holden wrote:
>>> Andrew Jaffe wrote:
>>>> The problem is that I actually do want to call these methods on the
>>>> class itself, before I've made any instances.
>>> Except you could use staticmethods with an explicit class argument ...
>> Yep, that would work! Thanks.
>> But it does seem like a bit of a kludge: classmethods seems to be
>> almost exactly what you 'ought' to use here (i.e., I really do want to
>> apply these methods to the class as an object in its own right).
> What's the use case?
Fair question. I hope the following isn't too technical.
I have a class which describes the model for fitting some data,
encapsulating among other things a bunch of parameters whose values I'd
like to determine for a given dataset. The base class is a simple model,
the derived class a slightly more complicated one with an extra parameter.
At present, I instantiate the class with a particular set of values for
One of the methods in this class is called 'prior', which returns the
prior probability for the instance's paramters.
However, it turns out there are some 'meta-parameters' which don't
change between instances, in particular the allowed limits on the
parameters, beyond which the prior should return 0. Currently these are
stored as class variables -- so both the base and derived class want to
be able to act as if these class variables are 'native' to their own
class -- since in fact the base/derived relationship is in this case
actually something of an implementation detail. (Currently I've solved
the problem by explicitly using the base class methods for setting the
Does this make sense?
More information about the Python-list