Creation of dictionaries in GlowScript 2.6

26 views
Skip to first unread message

Adam Hausknecht

unread,
Sep 3, 2017, 3:03:42 PM9/3/17
to Glowscript Users
Hi,

It seems that syntax for creating of a dictionary has changed in GlowScript 2.6!
I found that the statements

   D = {a: 1, b: 2}
   alert(D.a)

causes an "a" not defined error but

   D= {}
   D.a = 1
   D.b = 2

is ok! Is this only way to create a dictionary in GlowScript 2.6  or  is there another way using a list or lists of key and value pairs?

Thanks in advance,

Adam Hausknecht
Department of Mathematics
UMass Dartmouth


   

Bruce Sherwood

unread,
Sep 3, 2017, 4:24:43 PM9/3/17
to Glowscript Users
The preferred way to deal with dictionaries is to follow Python syntax, which is this:

D = {}
D['a'] = 1
D['b'] = 2
print(D['a'], D['b'])

or you can say

D = {'a':1, 'b':2}
print(D['a'], D['b'])

This code will work in standard Python as well as in GlowScript VPython. The only reason that D.a = 1 works is kind of an accident. That is a JavaScript construct, and it fails in standard Python. RapydScript-NG is a rather thin wrapper around JavaScript, so that occasionally something illegal in Python works in GlowScript VPython. Roughly speaking, a JavaScript dictionary (called an "object literal") is indistinguishable from an instance of a JavaScript class, whereas in standard Python these two things are different.

Bruce

Adam Hausknecht

unread,
Sep 4, 2017, 10:35:08 AM9/4/17
to Glowscript Users
 Bruce,

   OK! Thanks!

Adam 

 
Reply all
Reply to author
Forward
0 new messages