What is a classmethod for?

Kragen Sitaker kragen at pobox.com
Thu Jun 13 14:29:29 EDT 2002


Frank Tobin <ftobin at neverending.org> writes:
> On 10 Jun 2002, Kragen Sitaker wrote:
> > That's so you don't have to write this?
> > 
> > class HostPortAddress(tuple): pass
> > def HostPortAddress_from_str(s): 
> >     return HostPortAddress(s.split(':', 1))
> 
> Well, it makes sense to throw all the 'knowledge' of how to stringify and 
> de-stringify a HostPortAddress into one class.

Yes, that's reasonable.  But it seems like a small increment of
niceness to justify the conceptual complexity it adds to the language.

> > Are there times when it's more compelling?
> 
> Another example would be anytime where you have something that is close to
> a staticmethod which is a helper to the instance methods ,but needs some
> information that only the class can provide.

Helpers for instance methods can be instance methods themselves ---
there's no need for staticmethods or classmethods here.




More information about the Python-list mailing list