Why there is a parameter named "self" for classmethod function?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu May 7 02:29:57 EDT 2009


On Thu, 07 May 2009 00:39:28 -0400, Terry Reedy wrote:

> Functions that refer to neither the class nor an instance thereof can
> usually be moved outside the class altogether.  Python is not Java.  I
> believe staticmethod() was mainly added because it is needed for
> .__new__(), at least in some appearances.


Can be, but if there's reason enough to keep it with a class, there's no 
reason not to. Sometimes I have a class with a few methods that share 
common code, but that code happens to not include self (or cls). Since 
the common code is only of interest to that class, even though it doesn't 
need the instance, I factor the common code out into a method.

This would be a good candidate for a staticmethod, only due to laziness I 
don't usually bother :)


-- 
Steven



More information about the Python-list mailing list