[Python-ideas] Decorators for variables

Michael Selik mike at selik.org
Fri Apr 1 14:33:34 EDT 2016

On Fri, Apr 1, 2016 at 2:09 PM Matthias welp <boekewurm at gmail.com> wrote:

> > Function decorators
> There are decorators that return a callable that not calls the function
> that was given as an argument, but also do some other things, and therefore
> change the behaviour of that function.
> > So instead of
> >
> >   a = Char(length=10, value='empty')
> >
> > you want
> >
> >   @Char(length=10)
> >   a = 'empty'
> >
> > ?
> If possible, yes. So that there is a standardized way to access changing
> variables, or to put limits on the content of the variable, similar to the
> @accepts and @produces decorators that are seen here (
> https://wiki.python.org/moin/PythonDecoratorLibrary#Type_Enforcement_.28accepts.2Freturns.29
> )

There is a standardized way. You can extend ``property`` or mimic its
implementation. Then instead of

class Foo:
    a = property(getter, no_cycles)

You can write

class Foo:
    a = NoCycles()

I haven't linked to a how-to for doing this is, because I think it's
unnecessary for most small projects. Every so often someone asks for a more
pleasant syntax for specifying a property, getter and setter. Guido seems
to consistently reply that he thinks our current situation is good enough.
I'd dig up a link to the email archive for you, but Google wasn't being
very kind to me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160401/c3a6b504/attachment.html>

More information about the Python-ideas mailing list