Problem with Python objects

38 views
Skip to first unread message

Gilles Dubois

unread,
Jul 16, 2025, 6:14:39 AMJul 16
to VPython-users
The following program :
from vpython import *

scene.background=color.white*0.7
class revo:
    n=100
    da=2
    a=da*pi/180
    def norma(L):
        s = vec(0, 0, 0)
        for v in L:
            s = s + v
        return s / len(L)
    def __init__(self,min,max,f,col,op):
        self.xmin=min
        self.xmax=max
        self.fonc=f
        self.couleur=col
        self.opacity=op
        self.step=(max-min)/revo.n
    def P2D (self):
        return [(self.xmin + i * self.step, self.fonc(self.xmin + i * self.step)) for i in range(revo.n + 1)]

    def Vertices(self):
        Vert=[]
        for i in range(0, int(360 / revo.da)):
            V = [vertex(pos=vec(x[0], x[1], 0), normal=vec(0, 0, 1)) for x in self.P2D()]
            for v in V:
                v.rotate(axis=vec(0, 1, 0), angle=revo.a * i, origin=vec(0, 0, 0))
            Vert.append(V)
        V = [vertex(pos=vec(x[0], x[1], 0), normal=vec(0, 0, 1)) for x in self.P2D()]
        Vert.append(V)
        return Vert
    def Quads(self):
        LQ = []
        SV=self.Vertices()
        for k in range(0, int(360 / revo.da)):
            V1 = SV[k]
            V2 = SV[k + 1]
            for i in range(revo.n):
                s1 = V1[i]
                s2 = V1[i + 1]
                s3 = V2[i + 1]
                s4 = V2[i]
                LP = [s1.pos, s2.pos, s3.pos, s4.pos]
                N = revo.norma(LP)
                s1.normal = N
                s2.normal = N
                s3.normal = N
                s4.normal = N
                LQ.append(quad(vs=[s1, s2, s3, s4]))
        return LQ
    def nappe(self):
        scene.visible=False
        R = compound(self.Quads())
        R.color = self.couleur
        R.opacity = self.opacity
        scene.visible=True
        return R


def g(x):
    return x*x

R=revo(0,5,g,color.green,0.5)
scene.visible=True
REV=R.nappe()

works very well with installed python and Pycharm editor and represents the surface generated by a curve  y=f(x) turning around the y axis. (here a simple parabola y=x^2)
Now if I try it with Trinket
I have the error message 
Unexpected identifier 'self'
The same with web vpython, without any line number
If somebody can explain.
T

Steve Spicklemire

unread,
Jul 16, 2025, 8:32:52 AMJul 16
to VPython-users, Steve Spicklemire
Thanks Gilles,

I can’t exactly explain, but I think I may have discovered the basic problem.

I’ll say two things. First, I think this conversation probably belongs in the WebVPython group, since it’s really a WebVPython issue, so it might be helpful to raise it there. Second, I found a much simpler way to trigger the same issue:

------------------
Web VPython 3.2

def foo(x):
return 2*x

class bar:
def __init__(self, f):
self.f = f

def baz(self, check):
if check:
for x in range(2):
print(self.f(x))
# else:
# print([self.f(x) for x in range(2)])

b = bar(foo)

b.baz(True)
———————

If you uncomment the “else” clause in the “baz” method you’ll get the same error. I think it somehow arises because of the list comprehension mixed with a function object set as an attribute of the bar instance. I’ll dig into it a bit more when I get a chance. Thanks for the report!

-steve
> --
> You received this message because you are subscribed to the Google Groups "VPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/vpython-users/61427e1c-09a0-4353-b5c9-c5ccc0c5fde8n%40googlegroups.com.

Gilles Dubois

unread,
Jul 16, 2025, 12:15:25 PMJul 16
to VPython-users
In addition, making some tests I discovered that Web vpython wasn't comfortable with anonymous functions
This :
Web VPython 3.2 
g=lambda x:x*x
print(g(2))
doesn't work

but this 
Web VPython 3.2 WASM
g=lambda x:x*x
print(g(2))
is OK !
I discovered it trying to pass such a function as an argument to my revo object constructor

Now if we come back to the comprehension list issue.
This feature is very important for me 
it's elegant, readable, efficient, natural very close to the usual mathematical definitions of sets. It's a pity to use a for .. loop instead 

Gilles Dubois

unread,
Jul 16, 2025, 12:38:46 PMJul 16
to VPython-users
I completed the initial program with some widgets just to test
link
With Pycharm and Python installed color change and start/stop both work. 
Slider to change the function from x*x to A*x*x  doesn't work with ANY version (without any error message.)
With web python WASM it is even completely disabled and in addition start/stop button doesn't work (again without any error mention)



Gilles Dubois

unread,
Jul 17, 2025, 8:56:26 AMJul 17
to VPython-users
Last version
The program works with python 3.12 installed and Pycharm editor
Under web vpython WASM after first trial of the slider the program is entirely frozen.
Reply all
Reply to author
Forward
0 new messages