modifying __str__ and other dunder operators in Web VPython

15 views
Skip to first unread message

Joe Heafner

unread,
Oct 25, 2022, 6:54:57 PM10/25/22
to Web VPython Users
I've been experimenting with simple object oriented examples and have discovered that the following example works in command line Python but not in Web VPython. Specifically, modifying __str__ does not appear to work in Web VPython. I've looked at RapydScript's GitHub site for documentation on which dunder methods are and aren't supported. Have I missed something? I was working up to experimenting with overloading operators.

Web VPython 3.2

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __str__(self):
return f"{self.name}({self.age})"

p1 = Person("Joe", 25)

print(p1)
print(p1.name)
print(p1.age)


Joe Heafner
Sent from one of my Macs


Bruce Sherwood

unread,
Oct 25, 2022, 8:46:05 PM10/25/22
to Glowscript Users
I have no idea about __str__, but I will comment that there is no support for operator overloading in RapydScript because there is no support for operator overloading in JavaScript. The operator overloading of vectors in Web VPython is done with smoke and mirrors and doesn't offer a general solution. You might find this interesting:


Bruce

Joe Heafner

unread,
Oct 25, 2022, 8:55:30 PM10/25/22
to Web VPython Users

> On Oct 25, 2022, at 20:46, Bruce Sherwood <bruce.s...@gmail.com> wrote:
>
> I have no idea about __str__, but I will comment that there is no support for operator overloading in RapydScript because there is no support for operator overloading in JavaScript. The operator overloading of vectors in Web VPython is done with smoke and mirrors and doesn't offer a general solution. You might find this interesting:

Aha. Okay that tells me exactly what I needed to know.
Reply all
Reply to author
Forward
0 new messages