Python language problem

R

ripleyfu

class A:
.... pass
....<__main__.A instance at 0x00B91BC0>
I want to delete 'a' through 'b', why It does't?
How can I do that?
 
L

Laszlo Nagy

(e-mail address removed) i'rta:
... pass
...

<__main__.A instance at 0x00B91BC0>
I want to delete 'a' through 'b', why It does't?
How can I do that?
You must undestand that 'a' and 'b' are names. You can only delete
names, not objects. Objects are freed by the garbage collector,
automatically. Probably you used to write programs in C or Pascal or
other languages with pointers. In Python, there are no pointers, just
references and you cannot free an object. You can only delete the
references to it. The good question is: why would you like to free an
object manually? The garbage collector will do it for you automatically,
when the object has no more references. (Well, cyclic references are
also garbage collected but not immediately.)

If you need to handle resources, you can still use the try-finally
statement. Like in:

f = file('example.txt','r')
try:
s = f.read()
finally:
f.close() # The resource is freed. But the object that was used to
access the resource, may not be freed here....

Regards,

Laszlo
 
R

ripley

Laszlo said:
You must undestand that 'a' and 'b' are names. You can only delete
names, not objects. Objects are freed by the garbage collector,
automatically. Probably you used to write programs in C or Pascal or
other languages with pointers. In Python, there are no pointers, just
references and you cannot free an object. You can only delete the
references to it. The good question is: why would you like to free an
object manually? The garbage collector will do it for you automatically,
when the object has no more references. (Well, cyclic references are
also garbage collected but not immediately.)

If you need to handle resources, you can still use the try-finally
statement. Like in:

f = file('example.txt','r')
try:
s = f.read()
finally:
f.close() # The resource is freed. But the object that was used to
access the resource, may not be freed here....

Regards,

Laszlo

Thanks for your so detailed explain, I think I know you, But my Engish
is not enough
to explain.

I create same object in Tree, I want to update Tree, when I need to
delete subtree.
If where no references, I can't do that. some thing like blow:
for i in list:
del i
 
D

Diez B. Roggisch

Thanks for your so detailed explain, I think I know you, But my Engish
is not enough
to explain.

I create same object in Tree, I want to update Tree, when I need to
delete subtree.
If where no references, I can't do that. some thing like blow:
for i in list:
del i


This makes no sense. In python, objects are garbage collected when there are
no references to the anymore. What you do is

- create a reference to an object by letting i point to it

- remove i, such that the reference is removed.

This has no effect at all -- 1 - 1 = 0

If you want to remove items from a list, do something like this:

l[:] = [e for e in l if predicate(e)]

This will alter the list l such that only the elements of it that fullfill
the predicate are retained.

In other words:

your code above should work when written like this:

l[:] = []

which effectively removes all elements from the list.

This does NOT mean that the objects referenced to by the list are deleted!
If they are referenced from anywhere else, they are kept!!

Diez
 
S

Steve Holden

ripley said:
But 'b' is also deleted, i want use 'b' to delete 'a', 'b' is exists.
You can't do what you want to do.

Python names are independent references to objects. The "del" statement
deletes a name from a namespace (or an item from a structure), and
cannot be used to delete all references to a given object. In general
there's no way to delete a referenced object - we normally rely on the
implementation (in CPython reference counting plus garbage collection,
in other implementations just plain garbage collection) to perform the
deletion when no live references to an object remain.

Perhaps you'd like to explain *why* you find a need to do this (in other
words, what's your use case)?

Weak references are one possibility that might help you, but without
knowing your real requirements it's difficult to be more helpful.

regards
Steve
 
D

Dennis Lee Bieber

I create same object in Tree, I want to update Tree, when I need to
delete subtree.
If where no references, I can't do that. some thing like blow:
for i in list:
del i

Mainly because you are creating an "i" name each time, and then
removing just that "i" name. To delete something /inside/ a list, you
have to go into the container itself. The contents of a list are merely
references to the actual object. Of course, if you have an external name
somewhere, you still won't get rid of the object

while len(lst): #list is a built-in function, don't use as a name
del lst[0] #delete the first item IN the lst


a = someObject(1)
b = someObject(2)

lst = [ a, b ]

del lst[0] #removes first item in the list, but the name a is still
#around, so someObject(1) is still in memory

del b #removes the "b" name, but lst still has a reference to the
# someObject(2)

del lst[0] #removes the first (only) item in the list, a reference
#to someObject(2). No more references to someObject(2)
#so garbage collection gets rid of someObject(2)

del a #removes the only remaining reference to someObject(1)
#garbage collector gets rid of the object.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
R

ripley

Thanks all, you all are nice man.

Dennis Lee Bieber 写é“:

while len(lst): #list is a built-in function, don't use as a name
del lst[0] #delete the first item IN the lst

I found Dennis 's code is usefull that is a way to solve my problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top