Allow making python functions which would use `this` when called from js code
See original GitHub issueIn 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:
- Created 8 years ago
- Comments:11 (11 by maintainers)
Top 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 >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
Something like this seems to work:
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?
Since there was no reaction to this proposal, I remove the function this() and close this issue for the moment.