Deleting Objects

138 views
Skip to first unread message

Mark Schaedel

unread,
Feb 7, 2022, 9:41:27 AM2/7/22
to Glowscript Users
I sometimes have trouble removing objects from a list and also deleting them from memory. Here is a quick demo of how I am trying to do this. Can someone tell me what is wrong with my approach?


delAllButton = button(text = "Delete All", bind = delAll)
def delAll():
    for thisBall in ballList:
        thisBall.visible = False
        ballList.remove(thisBall)
        del thisBall

Bruce Sherwood

unread,
Feb 7, 2022, 12:06:10 PM2/7/22
to Glowscript Users
Using list.remove(object) does indeed remove the object from the list.

However, from the documentation at https://www.glowscript.org/docs/VPythonDocs/delete.html:

Deleting an Object

To hide a VPython object just make it invisible: ball.visible = False. This does not delete the object from computer memory, and you can make it visible again later.

Currently it is not possible to use del ball to delete a Web VPython object.

Bruce

P.S. In practice, the failure of Web VPython to delete a 3D object from memory isn't a problem, because a 3D object takes up only 40 bytes of memory.

Bruce Sherwood

unread,
Feb 7, 2022, 12:11:54 PM2/7/22
to Glowscript Users
I misspoke; a 3D object takes up about 120 bytes.

Bruce

Mark Schaedel

unread,
Feb 7, 2022, 12:31:30 PM2/7/22
to Glowscript Users
Why does my code only remove half(ish) of the balls and not all of them?

Bruce Sherwood

unread,
Feb 7, 2022, 1:22:40 PM2/7/22
to Glowscript Users
That's tricky, because repeated executions of "for thisBall in ballList:" are working with a changing ballList. This isn't a Web VPython problem; it's also a problem with installed Python.

Here is a way to do this that does remove all the elements:

L = [10, 20, 30, 40, 50, 60]
print('>', L)
N = len(L)
for i in range(N):
    L.remove(L[0]) # remove what is now the first element
    print('>', L)
print(L)

However, in your case the simple thing to do is simply to execute ballList = []

Bruce

Bruce Sherwood

unread,
Feb 7, 2022, 4:40:17 PM2/7/22
to Glowscript Users
Here's a slightly different version:

L = [10, 20, 30, 40, 50, 60]
print('>', L)
while len(L) > 0:

    L.remove(L[0]) # remove what is now the first element
    print('>', L)
print(L)

However, getting back to your actual case, it could look like this:

for o in ballList:
    o.visible = False
ballList = []

Bruce

陳冠榮

unread,
Feb 8, 2022, 10:45:05 AM2/8/22
to glowscri...@googlegroups.com

You may try the following codes:

 

delAllButton = button(text = "Delete All", bind = delAll)

def delAll():

    for thisBall in ballList:

        thisBall.visible = False

        del thisBall

    ballList.clear()

 

The way of for-loop you use is wrong. I used to have the same problem. You can trace the ballList, and find what is going on.

 

CKJ

 

Windows 郵件傳送

 

寄件者: Mark Schaedel
傳送時間: 202228 22:05
收件者: Glowscript Users
主旨: [glowscript-users] Deleting Objects

--

---
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/156fe619-70c4-4326-b6f0-ba5fa857c8fan%40googlegroups.com.

 

Mark Schaedel

unread,
Feb 8, 2022, 10:59:00 AM2/8/22
to Glowscript Users
Thank you Bruce and CKJ for your responses! 
Reply all
Reply to author
Forward
0 new messages