Set a variable as in setter

Mike Kazantsev mk.fraggod at gmail.com
Sun May 24 09:03:26 EDT 2009


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.

>    @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/0b056bd7/attachment-0001.sig>


More information about the Python-list mailing list