[Tutor] classmethod, staticmethod functions (decorator related)

Steven D'Aprano steve at pearwood.info
Mon Sep 13 13:05:10 CEST 2010


On Mon, 13 Sep 2010 12:29:07 pm Huy Ton That wrote:
> Hm, thanks guys; I just had to verify I was thinking sanely about it.
> I am going to pick up classmethods next. Do any of you have common
> design patterns for the usage. They are just items I haven't
> integrated in my coding, and I want to be certain I'm off on the
> right foot (:

The most common use for classmethods is to implement alternative 
constructors. For example, in the Decimal class starting in version 2.7 
and 3.1, you have two constructors:

>>> from decimal import Decimal
>>> Decimal("1.2345")
Decimal('1.2345')
>>> Decimal.from_float(1.2345)
Decimal('1.2344999999999999307220832633902318775653839111328125')

from_float is a class method.


-- 
Steven D'Aprano


More information about the Tutor mailing list