User loginNavigation |
Python metaprogramming with decoratorsThis recipe for using decorators to load data structures caught my eye (thanks to the Daily Python URL) as an interesting metaprogramming technique. Decorators are function transformers that can be applied declaratively to normal Python methods and functions using the much-disputed decorator syntax (see this previous LtU thread). This recipe uses decorators that return the supplied function unmodified, but as a side-effect register it in a dictionary of handlers for various value conversion operations. The decorator code itself is called when the module is loaded, so the dictionary is populated "on demand" at run-time. This makes an interesting contrast with compile-time metaprogramming techniques such as C++ template metaprogramming and the use of code generators and preprocessors. I do not believe that C# attributes could easily be used in a similar way, as unlike Python decorators these receive no information about the entities they are attached to when they are instantiated. |
Browse archives
Active forum topics |
Recent comments
22 weeks 6 days ago
22 weeks 6 days ago
22 weeks 6 days ago
45 weeks 19 hours ago
49 weeks 2 days ago
50 weeks 6 days ago
50 weeks 6 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago