method to create class property

Joe P. Cool joe.p.cool at googlemail.com
Tue Mar 18 16:15:15 EDT 2008


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)



More information about the Python-list mailing list