Possibility for method decorators?
See original GitHub issueI was thinking a bit about method decorators, and I just wanted to ask your opinion on the viability of this method.
What if, for decorated methods, instead of generating a function like this:
get funcName() {
return __get__ (this, function (self) {
// actual function innards
});
},
we did this:
function __impl__funcName(self) {
// actual function innards
},
get funcName() {
return __get__(this, this.__impl_funcName);
},
If I’m thinking correctly, this would allow us to then send __inner_funcName
through any decorators after we declare the class. I don’t know how well it would play with class decorators, but it would at least allow us to run the method through a python-like decorator and have the changes persist after class creation.
I haven’t dug too much into Transcrypt’s code generation yet, but if you think that this would be possible to do, I would definitely be willing to work on this feature.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:20 (19 by maintainers)
Top Results From Across the Web
Python: Decorators in OOP - Towards Data Science
The property decorator is very useful when defining methods for data validation , like when deciding if a value to be assigned is...
Read more >Class method decorator with self arguments? - Stack Overflow
There is always a possibility to create custom, class-specific decorator that will capture self and subsequently call the original decorator, passing runtime ...
Read more >Decorators in Python: A Complete Guide (with Examples)
Python decorators offer a way to extend the behavior of a function or method. Use decorators avoid repetition and improve the code quality....
Read more >PEP 318 – Decorators for Functions and Methods
The rationale for having a function that returns a decorator is that the part after the @ sign can be considered to be...
Read more >6. Decorators and Decoration | Advanced - Python Courses eu
A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi,
Seems you have solved this long standing problem at minimum expense! I still want to take a real good look, have to find the time for that. Won’t be long.
Kind regards Jacques
Based on your idea I made a compiler patch. I have no time to carefully test it, but for simple cases it works. Tested with this code:
Everything works, except static method when called from an instance, because it is passing self as first parameter. Until next week, I have no time to look at this case. But you can use my patch as a starting point. Btw, I fixed classmethod to use class, not instance just by reassigning first parameter to class itself. I know this is ugly, but at least it’s temporary solution to call class methods as in python.
P.S. seems that this classmethod fix will not work with with inherited classes, so we need another solution for such cases.
compiler.zip