PDA

View Full Version : [SOLVED] Pygame - Cannot Delete Sprite Object



Penguin Guy
March 8th, 2010, 05:17 PM
Simple case of not understanding variables properly:

Namespace rules apply to del - functions and classes can't delete outside variables
You use pop or remove to delete list items, not del


In my case, I had a list (opponents) that I was trying to delete from using a for loop and del:


for opponent in opponents:
del opponent


I just switched del opponent to opponents.remove(opponent), now everything works fine!


for opponent in opponents:
opponents.remove(opponent)


Thanks to Zugzwang (http://ubuntuforums.org/showpost.php?p=8940641) for the help. Fixed code attached.

Zugzwang
March 8th, 2010, 05:22 PM
A quick googling yields the following: http://www.python.org/search/hypermail/python-1994q3/0131.html

Short story: the "del" command removes some object from the namespace. However, what you are deleting is a parameter to the function. Thus this symbol is locally bound to the function anyway and it would be removed anyway when returning from the function. Of course the caller can have a local variable named "prey" which however remains untouched by the "del" statement as only the one in the function's namespace is removed.

Penguin Guy
March 8th, 2010, 06:25 PM
A quick googling yields the following: http://www.python.org/search/hypermail/python-1994q3/0131.html

Short story: the "del" command removes some object from the namespace. However, what you are deleting is a parameter to the function. Thus this symbol is locally bound to the function anyway and it would be removed anyway when returning from the function. Of course the caller can have a local variable named "prey" which however remains untouched by the "del" statement as only the one in the function's namespace is removed.
After moving the del statement outside the function, it still fails.

Zugzwang
March 9th, 2010, 07:28 PM
I don't quite get what you are trying to achieve with the "del" commands. Unlike in C/C++, you do not have to delete objects after you finished using them (to be precise: of course you only have the deallocate them if you allocated them manually using pointer stuff). "del" just removes the object under the stated name from the namespace. Here are some examples:



Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> A = "hello"
>>> print A
hello
>>> del A
>>> print A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>> A = [1,2,3]
>>> B = A[1]
>>> print B
2
>>> del B
>>> print A
[1, 2, 3]
>>> def myfunction(A):
... del A
...
>>> J = "hello"
>>> myfunction(J)
>>> print J
hello
>>>

As you can see, in the last example, the string is still accessible even after it was deleted. This is because only the object identifier "A" in the namespace of the function is removed, but the outer namespace remains intact.

In your code, I have seen that you iterate over some containers and then "del" their elements. This does *not* remove them from the container! You have to use other functions for doing so. Either you call "del" on the list and add the index of the element to delete or you use the "remove" function of the list. Note that it generally not a good idea to remove an element from the list you iterate over, but I'm quite sure that you favourite search engine has some tricks in its database.

Penguin Guy
March 9th, 2010, 07:59 PM
I don't quite get what you are trying to achieve with the "del" commands. Unlike in C/C++, you do not have to delete objects after you finished using them (to be precise: of course you only have the deallocate them if you allocated them manually using pointer stuff). "del" just removes the object under the stated name from the namespace. Here are some examples:

A string is still accessible even after it was deleted. This is because only the object identifier "A" in the namespace of the function is removed, but the outer namespace remains intact.

In your code, I have seen that you iterate over some containers and then "del" their elements. This does *not* remove them from the container! You have to use other functions for doing so. Either you call "del" on the list and add the index of the element to delete or you use the "remove" function of the list. Note that it generally not a good idea to remove an element from the list you iterate over, but I'm quite sure that you favourite search engine has some tricks in its database.

Got it - all variables are pointers. Thanks, I can't believe I got this far with Python without knowing that. I just had to replace del opponent for opponents.remove(opponent)!