[Tutor] classmethod, staticmethod functions (decorator related)

Huy Ton That huyslogic at gmail.com
Mon Sep 13 15:53:31 CEST 2010


Thank you all,

I was taking a look at the module decimal.py as you cited, and it makes
sense now. Looks very useful to make tools without having to instantiate
anything.

On Mon, Sep 13, 2010 at 7:05 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100913/20751c29/attachment.html>


More information about the Tutor mailing list