question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possibility for method decorators?

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:20 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
JdeHcommented, Sep 18, 2017

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

2reactions
neriusmikacommented, Sep 15, 2017

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:

def method_decorator(method):

    def wrapper(instance, a, b):
        console.log('from method wrapper')
        return method(instance, a * 2, b * 2)

    return wrapper


def class_decorator(arg):

    def wrapper(cls):
        # console.log(cls)
        # console.log(arg)
        console.log('from class wrapper')
        return cls

    return wrapper


@class_decorator('abc')
class MyClass:

    def __init__(self):
        self.variable = '123'

    @method_decorator
    def mymethod(self, a, b):
        return a + b

    @classmethod
    def myclassmethod(cls):
        console.log(cls.variable)  # must be undefined
        console.log(cls.__name__)

    @staticmethod
    def mystaticmethod(a, b):
        console.log(a, b)


myobj = MyClass()
result = myobj.mymethod(2, 3)
console.log(result)
myobj.myclassmethod()
MyClass.myclassmethod()
myobj.mystaticmethod(4, 5)
MyClass.mystaticmethod(4, 5)

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found