Web VPython range function behavior differs from standard Python

83 views
Skip to first unread message

Daniel Schroeder

unread,
Dec 10, 2024, 11:11:20 PM12/10/24
to Glowscript Users
Pardon me if this is already well known.

I've just discovered an interesting difference between Web VPython and standard Python.

If n = 10 as you go into a loop that starts with "for i in range(n)" and you then change the value of n to 20 during one of the loop iterations, standard Python executes the loop just 10 times because the range function is evaluated just once at the start. But Web VPython executes the loop 20 times, behaving as if you've written a JavaScript (or C, etc.) loop of the form "for (i=0; i<n; i++)".


This doesn't surprise me because I know Web VPython is really JavaScript in disguise. But it might surprise some people.

Does anyone maintain a list of differences between Web VPython and standard Python?

Dan

Bruce Sherwood

unread,
Dec 11, 2024, 2:18:43 PM12/11/24
to Glowscript Users
Dan, I don't know of such a list. Recently a user reported that randint(x,y) never generates x if x is a negative number. Another difference is that 1/0, which gives an error in true Python, returns Infinity.

Bruce

Harlan Gilbert

unread,
Dec 12, 2024, 9:29:46 PM12/12/24
to glowscri...@googlegroups.com
Third difference: Glowscript prints largish integers (greater than 9999) using scientific notation by default.

A list might make sense.

Harlan Gilbert, Ph.D.
High School Math, Physics, Computer Science, and Philosophy Teacher
Collegium Chair
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977


On Wed, Dec 11, 2024 at 2:18 PM Bruce Sherwood <bruce.s...@gmail.com> wrote:
Dan, I don't know of such a list. Recently a user reported that randint(x,y) never generates x if x is a negative number. Another difference is that 1/0, which gives an error in true Python, returns Infinity.

Bruce

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/glowscript-users/3f2d4ad3-7ba8-45f7-ab21-5ac1a1ef093dn%40googlegroups.com.

Daniel Schroeder

unread,
Dec 12, 2024, 9:46:14 PM12/12/24
to Glowscript Users
Some other differences I've noticed over the years:

* Standard Python requires that a function be defined above (in the source file) where it is first used. Web VPython (like JavaScript) has no such constraint, so you can put function definitions anywhere.
* Standard Python doesn't allow "string" + number, whereas Web VPython will automatically convert the number to a string and concatenate them.
* In standard Python, the only allowed logical not operator is "not"; in Web VPython you can also use !.
* In Web VPython you can extend a list by assigning a value to a list element whose index is beyond the current range.  In standard Python this generates an error.
* In Web VPython you can say x[i/2] as long as i is an even integer at runtime.  In standard Python this generates an error, so you have to say x[int(i/2)].

I suspect there are many more.

Dan

Bruce Sherwood

unread,
Dec 12, 2024, 10:40:13 PM12/12/24
to Glowscript Users
I should mention that Steve Spicklemire has experimented with the possible use of pyodide, a project aimed at producing a version of true Python that runs in the browser (https://pyodide.org/en/stable/). It is conceivable that we could use this rather than using the RapydScript-NG Python-to-JavaScript transpiler. I'm not sure, but I think there may currently be issues with execution speed when using pyiodide.

Bruce

Daniel Schroeder

unread,
Dec 12, 2024, 10:43:56 PM12/12/24
to Glowscript Users
I'll choose performance over perfect compatibility any day!

But I would suggest creating a help page that outlines the differences.

Dan

Harlan Gilbert

unread,
Dec 16, 2024, 11:41:50 PM12/16/24
to glowscri...@googlegroups.com
Bruce,

randint(1,10) worked yesterday, but does not today.  Did something change?

Thanks,
Harlan

On Wed, Dec 11, 2024 at 2:18 PM Bruce Sherwood <bruce.s...@gmail.com> wrote:
Dan, I don't know of such a list. Recently a user reported that randint(x,y) never generates x if x is a negative number. Another difference is that 1/0, which gives an error in true Python, returns Infinity.

Bruce

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/glowscript-users/3f2d4ad3-7ba8-45f7-ab21-5ac1a1ef093dn%40googlegroups.com.


--
Message has been deleted

Bruce Sherwood

unread,
Dec 17, 2024, 2:35:05 AM12/17/24
to Glowscript Users
Works for me.

Bruce

Harlan Gilbert

unread,
Dec 19, 2024, 9:34:12 PM12/19/24
to glowscri...@googlegroups.com
Yes, sorry, I forgot that randint needed to be imported. 
Two more behaviors for the list:
random is the one module it is possible to import from, and 
- the random function itself works without being imported


Harlan Gilbert, Ph.D.
High School Math, Physics, Computer Science, and Philosophy Teacher
Collegium Chair
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977


On Tue, Dec 17, 2024 at 2:35 AM Bruce Sherwood <bruce.s...@gmail.com> wrote:
Works for me.

Bruce

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
Message has been deleted
Message has been deleted

Germain P

unread,
Jan 3, 2025, 7:06:44 AMJan 3
to Glowscript Users
Hello everybody, I have spotted a couple more differences between standard python and WebVPython. I use Python 3.9.13 and vpython 7.6.4 when executing programs locally on my computer. The version of WebVPython I have been using is 3.2. 

– WebVPython 3.2 does not like parentheses in the arguments of list comprehensions: 

def f(x):
    return 3*x
list_with_parentheses = [f(i) for i in range(3)]

will yield SyntaxError: Unexpected identifier 'f', which I understand is a JavaScript error. 

– I have not found a way to define a tuple object on WebVPython:

t = (2,2,3)

creates a list. The tuple constructor, that I contemplated as a workaround, is not supported by WebVPython: 

t=tuple([2,2,3]) 

does not work.

Bruce Sherwood

unread,
Jan 3, 2025, 4:46:10 PMJan 3
to Glowscript Users
The key point is that JavaScript does not have tuples.

Bruce

Harlan Gilbert

unread,
Jan 10, 2025, 7:54:46 AMJan 10
to glowscri...@googlegroups.com
One more difference, due to the conversion to JavaScript:
In Python, integer operations are unlimited-precision.  In Glowscript, you cannot get more than about 16 or 17-digits of accuracy (2^53).

On Fri, Jan 3, 2025 at 4:46 PM Bruce Sherwood <bruce.s...@gmail.com> wrote:
The key point is that JavaScript does not have tuples.

Bruce

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages