Comments (8)
-
is a documentation bug, now fixed.
-
Not sure what you mean. If you dont delare a nonlocal variable in the outer scope in python, what value would it take in the inner scope? For example, running the following python3 script, gives a SyntaxError
def f():
nonlocal a
print(a)
SyntaxError: no binding for nonlocal 'a' found
rs-ng generates for the same snippet
function f() {
print(a);
};
which will print nothing since a is undefined.
from rapydscript-ng.
I'm puzzled, Kovid. When I run your own README example in Python 3,
a = 1
b = 1
def increment():
nonlocal a
a += b
increment()
print(a)
I get the error "SyntaxError: no binding for nonlocal 'a' found". I have to change nonlocal to global to get the expected result of a = 2. Shouldn't RapydScript support both the standard Python global and nonlocal options, rather than redefine the meaning of nonlocal? What am I missing?
from rapydscript-ng.
Ah I get your confusion -- python has this crazy system where variables in the "global" i.e. module scope are special and need to use global keyword while variables in an outer scope, but not the global scope need the nonlocal keyword. RS simplifies this unnecessarily baroque system to just have nonlocal which works with any scope regardless of whether it is the global or only outer scope. So the only incompatibility between python and rs is when you have a global variable that is used in an inner scope but is also shadowed in an outer scope. In python, you can ignore the outer scopes and go direct to the module scope by using global instead of nonlocal in RS it will always use the variable from the next outer scope, there is no way to bypass all scopes and go to module scope.
from rapydscript-ng.
Thanks much for this explanation, which should (in some form) be added to the README documentation. You have a good logical argument for extending nonlocal. However, in the GlowScript case it's also important to support global, which I think I can do in the fairly extensive preprocessing I do before sending the code to rapydscript.
The reasons for needing to support global is that we try hard to make the same program work whether it runs in GlowScript VPython (all browser with RS) or in Jupyter VPython (Jupyter notebook with true Python), limited of course to not using a module such as numpy, and global is a commonly used Python element. Moreover, the following should work, and print "5", whereas with nonlocal, "a" must be initialized outside the function, which is mildly inconvenient:
def f():
global a
a = 5
f()
print(a)
What I can do is detect "global a", change this to "nonlocal a", and insert at the start of the program "a = None". It would be better though if RS supported global, while continuing to support the extended version of nonlocal. Currently I do change global -> nonlocal before the RS transpiling, but I should also set the variables to None at the start of the program. You've helped me see what I need to do. I realize that this won't work for global in an embedded function, but that case is unlikely in the VPython usage situation, and I can also give an error message.
from rapydscript-ng.
I can certainly see why you would need global -- however it's a fair bit of effort to add it to RS for relatively little gain, given the way nonlocal works in RS, so while I dont object to adding it, it's also not much a priority for me, patches are welcome!
from rapydscript-ng.
Also, using global without declaring the variable in the global namespace is a pretty bad code pattern, for instance, in python3, the following code will raise a SyntaxError
def f():
global a
a = 1
print(a)
If you dont declare the variable in the global scope, you have to ensure some function that uses it is called before the referenced variable is used anywhere -- which is very fragile for obvious reasons.
from rapydscript-ng.
Wow! You are remarkable, Kovid. Your dedication to your users is amazing, as is the speed of your response. Thank you for global.
I of course am using the embedded version of the transpiler; has that been updated to support global? How would I know whether/when the embedded version is up-to-date with the rapydscript-ng repository?
As for "patches are welcome", I would love to contribute to RS. It has made a huge difference in my professional life, because it made it possible for programming novices, including in particular the many students who use the physics textbook that Ruth Chabay and I wrote, to do their work at glowscript.org without having to install anything, which greatly increased their enthusiasm for using VPython. At the moment I can't contribute directly due to many pressures that include a major cross-country move Ruth and I made a few weeks ago. I am however trying to make RS in general and RS-NG in particular much better known.
I was unaware of the behavior in Python of
def f():
global a
a = 1
print(a)
from rapydscript-ng.
I just realized there was an easy way to implement partial global support (basically it works just like nonlocal except it also declares the variables at the top level of the module -- see the changes to README for the caveat).
The embedded compiler is auto-updated every time a new rs-ng release is made. You can always generate your own from a source checkout, instructions are in the README.
from rapydscript-ng.
Related Issues (20)
- Import problem? HOT 4
- class decorator bug HOT 5
- when converting .children() to list, i get list of empty dictionaries HOT 4
- Remove typing/annotations from input HOT 1
- Ellipsis HOT 7
- bitops on booleans HOT 1
- string.split() HOT 3
- random.randint tolerates 3rd argument HOT 3
- distionary += error HOT 21
- Build question HOT 5
- pop for dict, pypop for list HOT 8
- Idle curiosity about duplication HOT 1
- Missing element from rapydscript.js?
- Mistranspilation HOT 2
- Remove a list from a list HOT 4
- while/else HOT 3
- randrange error
- Function argument handling
- Comical Bundle Size HOT 1
- Large slowdown HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rapydscript-ng.