Idea For Attributes/Annotations

Daniel Keep daniel.keep.lists at gmail.com
Tue Mar 10 20:20:18 PDT 2009


For reference, Python has a concept called decorators.  Take this function:

> def trace(fn):
>     def __wrap(*pargs,**kwargs):
>         print 'IN  %s' % fn.__name__
>         r = fn(*pargs,**kwargs)
>         print 'OUT %s' % fn.__name__
>         return r
>
>     return __wrap

And use it like so:

> @trace
> def foo(msg):
>     print 'Bar: %s' % msg
>     return 42
>
> print foo("baz")

It produces this output:

> IN  foo
> Bar: baz
> OUT foo
> 42

And is equivalent to the following:

> def foo(msg):
>     print 'Bar: %s' % msg
>     return 42
>
> foo = trace(foo)

I believe either 2.5 or 3.0 adds this syntax to classes as well.

  -- Daniel



More information about the Digitalmars-d mailing list