BruceSherwood / glowscript

This repository has been moved to https://github.com/vpython/glowscript
50 stars 17 forks source link

Printing the type of a vector instance throws an error #216

Closed zimmermant closed 5 years ago

zimmermant commented 5 years ago

Trying to find the type of a vector instance doesn't work in glowscript but does work in VPython. For example,

A=vector(0,0,0)
print(type(A))

results in the following error in Glowscript 2.3 or later

function attributeVectorPos(parent,x,y,z){this.__parent=parent;this.__x=x;this.__y=y;this.__z=z;if(parent){parent.__change();parent._pos_set=true;if(parent.__make_trail)parent.__update_trail(vec(x,y,z))}}

In VPython this returns <class 'vpython.cyvector.vector'>, which is what I'd expect.

BruceSherwood commented 5 years ago

Thanks for the report. In VPython 7 I get <class 'cyvector.vector'>, not what you get. What version of the vpython module are you using? What version of Python? I'm using vpython 7.5.0 with Python 3.7.

zimmermant commented 5 years ago

I've got VPython 7.5.0 with Python 3.6.8.

BruceSherwood commented 5 years ago

The GlowScript problem is an artifact of the RapydScript-NG Python-to-JavaScript transpiler, but I think I may be able to address the problem by supplying my own type() function.

I wouldn't expect a difference between Python 3.6.8 and Python 3.7. I'm running on Windows; what platform are you using?

BruceSherwood commented 5 years ago

Also, how do you launch the VPython 7 program? IDLE? Spyder? Jupyter notebook?

I've been testing my own replacement for type() and the only question left is this discrepancy between what you see for vec and what I see.

Also, could you tell me how you use the type() function? I would have thought that isinstance() would be more useful, and I've not used type() myself, as far as I can remember.

zimmermant commented 5 years ago

I'm running it on a Mac with macOS 10.14.4. I am running the command in a Jupyter notebook (ipython version 7.5.0 and jupyter-client version 5.2.4).

What I used was just

a=vector(1,2,3)
print(type(a))

I wanted to use isinstance() but wasn't sure what string to enter for the class, so I was using the type() command to try and figure that out.

BruceSherwood commented 5 years ago

I've updated GlowScript VPython so that the type() function now does useful things. I arbitrarily return the Windows form <class 'cyvector.vector'> rather than the Mac form <class 'vpython.cyvector.vector'>, but the key point is that the final term is "vector". For box (etc.) and int and float and sqrt and bool and list and dict and str I now return what you see in VPython 7. I wrote my own subroutine and redefined type() to point to my subroutine. Thanks much for you help.