[Python-ideas] setting function properties with a decorator syntax

David Stanek dstanek at dstanek.com
Fri Mar 19 03:28:46 CET 2010


On Thu, Mar 18, 2010 at 2:49 AM, nbv4 <nbvfour at gmail.com> wrote:
>
> @printable=True
> def some_func(*args, **kwargs):
>    return "sup"
>
> Any thoughts?
>

I don't remember seeing much code using function properties. I would
probably just create a new decorator for this.

    >>> def add_props(**kwargs):
    ...     def _(func):
    ...         for key in kwargs:
    ...             setattr(func, key, kwargs[key])
    ...         return func
    ...     return _
    ...
    >>> @add_props(printable=True)
    ... def f():
    ...     """Example function"""
    ...
    >>> f.printable
    True
    >>>


-- 
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek



More information about the Python-ideas mailing list