Error: Using functions in list comprehension

47 views
Skip to first unread message

comment comments

unread,
May 30, 2023, 11:51:51 AM5/30/23
to Glowscript Users
Any time I use a function in a list comprehension, it gives me a " SyntaxError: Unexpected identifier 'fnName' "

For example:
Web VPython 3.2
def asdf():
    pass
[asdf() for a in [1,2,3]]

gives the error "SyntaxError: Unexpected identifier 'asdf'"

comment comments

unread,
May 30, 2023, 12:31:57 PM5/30/23
to Glowscript Users
Adding info: it appears that (at least some) glowscript functions work as expected (i.e. [sphere(pos=a) for a in [vector(1,2,3),vector(1,1,1), vector(0,0,0)]] gives three spheres), but (at least some) python functions do not (i.e. [sum(a) for a in [[1,2,3],[1,1,1],[0,0,0]]] gives an error)

Harlan Gilbert

unread,
May 30, 2023, 12:47:25 PM5/30/23
to glowscri...@googlegroups.com
Your example works properly through VPython version 2.8, in versions thereafter throws an error.

--

---
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 on the web visit https://groups.google.com/d/msgid/glowscript-users/d6b883b1-25a9-44b5-b992-b4baf236a707n%40googlegroups.com.


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

Joe Heafner

unread,
May 30, 2023, 1:00:12 PM5/30/23
to Web VPython Users


> On May 30, 2023, at 12:47, Harlan Gilbert <hgil...@gmws.org> wrote:
>
> Your example works properly through VPython version 2.8, in versions thereafter throws an error.

RapydScript (https://github.com/kovidgoyal/rapydscript-ng) doesn't support nested list comprehensions, but that doesn't seem to be what's going on here. I wonder if it's a bug elsewhere.


Joe Heafner
Sent from one of my Macs


Bruce Sherwood

unread,
May 31, 2023, 1:18:36 PM5/31/23
to Glowscript Users
When I run the following in installed Python

def asdf():
    pass
c = [asdf() for a in [1,2,3]]
print(c)

I get the result [None, None, None]. Which leaves me wondering what is intended. Can you provide a simple test routine that produces something other than a list of Nones when running with installed Python?

Harlan Gilbert

unread,
May 31, 2023, 9:13:22 PM5/31/23
to glowscri...@googlegroups.com
This would be a more normal usage of list comprehension

def sqs(x):
    return x**2
c = [sqs(a) for a in [1,2,3]]
print(c)


--

---
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.

Bruce Sherwood

unread,
Jun 1, 2023, 12:43:34 AM6/1/23
to Glowscript Users
Thanks for the help. The problem is that the generated JavaScript code contains inappropriate async and await statements . I suspect that it will be difficult to fix this. The workaround of course is to use a loop.

Bruce
Reply all
Reply to author
Forward
0 new messages