On Sun, 24 May 2009 19:03:26 +0600
Mike Kazantsev <mk.frag...@gmail.com> wrote:

> On Sun, 24 May 2009 05:06:13 -0700 (PDT)
> Kless <jonas....@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

Attachment: signature.asc
Description: PGP signature

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to