Declarative properties

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 11 14:27:35 CEST 2007


On Thu, 11 Oct 2007 11:48:18 +0000, Artur Siekielski wrote:

> class Person(object):
>    def __init__(self, name):
>       self._name = name
>    def _get_name(self):
>       return self._name
>    def _set_name(self, new_name):
>       self._name = new_name
>    name = property(_get_name, _set_name)

This is more easily spelled:

class Person(object):
    def __init__(self, name):
        self.name = name

> I would like to have something like that:
> 
> class Person(object):
>    name = property('_name')
> 
> I assume that this causes "generation" of instance field '_name' and
> default getters and setters.

But why?  Default getters and setters are unnecessary and if you need
something other than the default you need to write it anyway more
explicitly.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list