Welcome, guest | Sign In | My Account | Store | Cart

This property shortcut doesn't restrict the property's attributes to get, set, del and doc. Besides these functions can have any name, not limited to fget, fset ... Note that the default doc is not the getter's one.

Python, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def propertx(fct):
    '''
        Decorator to simplify the use of property.
        Like @property for attrs who need more than a getter.
        For getter only property use @property.
    '''
    arg=[None, None, None, None]
    for i, f in enumerate(fct()):
        arg[i] = f
    if not arg[3] :
        arg[3]=fct.__doc__
    return property(*arg)

if __name__=='__main__':

    class example(object):
        def __init__(self):
            self._a=100
        @propertx
        def bar():
            # BAR doc
            def get(self):
                return self._a
            def set(self, val):
                self._a=val
            return get, set

    foo=example()
    print foo.bar
    foo.bar='egg'
    print foo.bar

2 comments

Ian Bicking 17 years, 2 months ago  # | flag

with classes... The recipe here might be of interest: http://svn.colorstudy.com/home/ianb/recipes/propertyclass.py

It implements a property class that can be subclassed to create concrete properties.

runsun pan 14 years, 11 months ago  # | flag

This might be of interest:

Easy Property Creation in Python http://code.activestate.com/recipes/576742/

Only 7 lines of code, easy to understand, easy to use, easy to customize, save you a lot of typing

Created by Louis RIVIERE on Wed, 21 Feb 2007 (MIT)
Python recipes (4591)
Louis RIVIERE's recipes (13)

Required Modules

  • (none specified)

Other Information and Tasks