method to create class property

Diez B. Roggisch deets at nospam.web.de
Tue Mar 18 21:59:58 CET 2008


Joe P. Cool schrieb:
> Hi,
> 
> I like C#'s style of defining a property in one place. Can the
> following way
> to create a property be  considered reasonable Python style (without
> the
> print statements, of course)?
> 
> class sample(object):
>     def __init__(self):
>         sample.y = self._property_y()
> 
>     def _property_y(self):
>         def _get(self):
>             print 'getting y.'
>             return self._y
>         def _set(self, value):
>             print 'setting y.'
>             self._y = value
>         def _del(self):
>             print 'bye, y!'
>             del self._y
>         return property(_get, _set, _del)



There are a few recipies, like this:


class Foo(object):

     @apply
     def foo():
         def fget(self):
             return self._foo
         def fset(self, value):
             self._foo = value
         return property(**locals())


Diez



More information about the Python-list mailing list