More __init__ methods

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Nov 6 15:45:43 EST 2008


Mr.SpOOn <mr.spoon21 at gmail.com> writes:

> On Thu, Nov 6, 2008 at 4:59 PM, Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
> > class A(object):
> >    def __init__(self, a, b, c):
> >        self.a = a
> >        # ...
> >
> >    @classmethod
> >    def from_string(cls, s):
> >        # ...
> >        return cls(a, b, c)
> 
> Thanks.
> I think it's time to study decorators.

Studying decorators is a good idea, but in this instance it's not
necessary. The above is merely using decorator syntax to apply the
‘classmethod’ type to a function.

All you need to know to understand the above is that it will have
essentially the same result as:

    class A(object):
        # ...

        def _from_string(cls, s):
            # ...
            return cls(a, b, c)
        from_string = classmethod(_from_string)
        del _from_string

without the intermediate binding of the ‘_from_string’ name.

-- 
 \          “The best way to get information on Usenet is not to ask a |
  `\               question, but to post the wrong information.” —Aahz |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list