Set a variable as in setter

Duncan Booth duncan.booth at invalid.invalid
Sun May 24 14:27:13 CEST 2009


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

What's wrong with just doing this?:
        self.bar = bar

> 
>    @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'
> -------------------
> 




More information about the Python-list mailing list