SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
НазадМетки: python
I think it’s safe to say that the goal of macros in a language is to provide a way to modify elements of the language. That’s what decorators do in Python – they modify functions, and in the case of class decorators, entire classes. This is why they usually provide a simpler alternative to metaclasses.
The major failings of most language’s self-modification approaches are that they are too restrictive and that they require a different language (I’m going to say that Java annotations with all the hoops you must jump through to produce an interesting annotation comprises a “different language”).
Python falls into Fowler’s category of “enabling” languages, so if you want to do modifications, why create a different or restricted language? Why not just use Python itself? And that’s what Python decorators do.