Set a variable as in setter
Mike Kazantsev
mk.fraggod at gmail.com
Sun May 24 09:14:54 EDT 2009
On Sun, 24 May 2009 19:03:26 +0600
Mike Kazantsev <mk.fraggod at gmail.com> wrote:
> On Sun, 24 May 2009 05:06:13 -0700 (PDT)
> Kless <jonas.esp at googlemail.com> wrote:
>
> > Is there any way to simplify the next code? Because I'm setting a
> > variable by default of the same way than it's set in the setter.
> >
> > -------------------
> > class Foo(object):
> > def __init__(self, bar):
> > self._bar = self._change(bar) # !!! as setter
>
> Guess it's obvious, but why not use "setattr(self, 'bar', bar)" here, in
> __init__ - it'll just call defined setter.
In fact, "self.bar = bar" is even simplier.
Somehow I thought it wouldn't work here, but it does.
> > @property
> > def bar(self):
> > return self._bar
> >
> > @bar.setter
> > def bar(self, bar):
> > self._bar = self._change(bar) # !!! as in init
> >
> > def _change(self, text):
> > return text + 'any change'
> > -------------------
>
--
Mike Kazantsev // fraggod.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090524/13b2194b/attachment-0001.sig>
More information about the Python-list
mailing list