TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.82k stars 215 forks source link

How to compare two methods? #863

Open chopin opened 9 months ago

chopin commented 9 months ago

Hi.

I want to check whether a given callback is a specific method in a class. I found that comparing two methods failed. Following is the test code:

class ClassA:
    def proc(self, callback=None):
        if callback==self.method1:
            print('callback==self.method1, callback=', callback)
        else:
            print('callback!=self.method1')
            print('callback.name=', callback.name)
            print('callback.__name__=', callback.__name__)
            print('callback.__qualname__=', callback.__qualname__)
            print('callback=',  callback)
            print('self.method1=', self.method1)
        if self.method1==self.method1:
            print('self.method1==self.method1')
        else: print('self.method1!=self.method1')
    def method1(self):
        pass
def main():
    a1= ClassA()
    a1.proc(a1.method1)
if __name__ == '__main__':
    main()

output:

callback!=self.method1
callback.name= None
callback.__name__= None
callback.__qualname__= None
callback= function () {
                var args = [] .slice.apply (arguments);
                return func.apply (null, [self] .concat (args));
            }
self.method1= function () {
                var args = [] .slice.apply (arguments);
                return func.apply (null, [self] .concat (args));
            }
self.method1!=self.method1

What I was surprised was the last line in the output, self.method1!=self.method1, which was a self comparison, but failed. (Is it a bug?)

How can I know a given callback is equal to a specific method? I tried to find a workaround. But as the output above, information about the method (name, name , and qualname ) is not available. Any idea or workaround is welcome if you have any.

Thanks.