简体   繁体   中英

VPython 6 - Object won't delete

I have a 3D elastic collision simulation. I'd like to clear all of the sphere objects from my program from a button press. The documentation indicates that I should do the following:

def clear_balls():
   for ball in balls:
      ball.visible = False
      del ball

This successfully makes the balls invisible in the scene, but they still take up memory and collide with balls that still exist. I want it completely removed. Trying this with a unique ball name this isn't part of a list as some have suggested still results in the same issue.

del ball is not doing what you think because balls still holds a reference to the object. You need to empty the balls list:

def clear_balls():
    for ball in balls:
        ball.visible = False
     balls[:] = []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM