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.

Allow making python functions which would use `this` when called from js code

See original GitHub issue

In js you can write something like this:

obj = {
    f: function(){
        this.do_something
    }
}
obj.f()

Suppose there is js code that you want to mix with python and which relies on this behaviour (expects to be able to call obj.f). Attempt to port obj creation to python would not work because python doesn’t have implicit this. I can see two possible solutions. First: create wrapper which would transform python function so that when it’s called from js it would receive this as first argument.

def f(self):
    self.do_something
obj = {
    'f': js_pass_this(f)
}
# or use js_pass_this as decorator

Second: create get_this() function.

def f():
    get_this().do_something
obj = {
    'f': f
}

I think first solution is both better and easier to implement though.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
caryosceluscommented, Mar 19, 2016

Something like this seems to work:

function pass_this(f) {
    new_f = function(){
        var args = [this]
        for (var i = 0; i < arguments.length; ++i) {
            args.push(arguments[i])
        }
        f.apply(null, args)
    }
    new_f.$infos = f.$infos
    return new_f
}
pass_this.$infos = {
        __name__:"foo",
        __defaults__ : [],
        __module__ : "__main__",
        __doc__: None,
        __annotations__: {},
        __code__:{
        __class__:$B.$CodeDict,
        co_argcount:1,
        co_filename:'js',
        co_firstlineno:1,
        co_flags:67,
        co_kwonlyargcount:0,
        co_name: "pass_this",
        co_nlocals: 1,
        co_varnames: ["f"]}
}

Obviously, this isn’t proper way to introduce new brython function, but i don’t know what is for such function anyway. I presume it should be placed in javascript module, not sure what is the best name though. Any thoughts?

0reactions
PierreQuentelcommented, Jun 10, 2016

Since there was no reaction to this proposal, I remove the function this() and close this issue for the moment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Call Python function from JavaScript code - Stack Overflow
Javascript runs on the client. I assume the python runs on the server. You could send an ajax request to the server. It...
Read more >
Python Functions – How to Define and Call a Function
In this article, I will show you how to define a function in Python and call it, so you can break down the...
Read more >
Tutorial: Basic Functions in Python - CodeHS
Variables created inside a function, including parameter variables, can only be used inside the function. Take a look at this example in the...
Read more >
Defining Your Own Python Function
In this tutorial, you'll learn how to define and call your own Python function. You'll also learn about passing data to your function,...
Read more >
Higher Order Functions in Python - GeeksforGeeks
It allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the...
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