>>> class A:
.... pass
.... a = A() b = a del b a
<__main__.A instance at 0x00B91BC0>
I want to delete 'a' through 'b', why It does't?
How can I do that? 8 1228 ri******@gmail.com wrote: class A: ... pass ... a = A() b = a del b a
<__main__.A instance at 0x00B91BC0> I want to delete 'a' through 'b', why It does't? How can I do that?
del a,b ri******@gmail.com i'rta: class A: ... pass ... a = A() b = a del b a
<__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
Boris Borcic wrote: ri******@gmail.com wrote:> class A: ... pass ...> a = A() > b = a > del b > a <__main__.A instance at 0x00B91BC0> I want to delete 'a' through 'b', why It does't? How can I do that?
del a,b
But 'b' is also deleted, i want use 'b' to delete 'a', 'b' is exists.
Laszlo Nagy wrote: 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 iFrom that code, I can't update list anymore.
> 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
ripley wrote: Boris Borcic wrote:
ri******@gmail.com wrote:
>>class A:
... pass ...
>>a = A() >>b = a >>del b >>a
<__main__.A instance at 0x00B91BC0> I want to delete 'a' through 'b', why It does't? How can I do that?
del a,b
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro...
|
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
by: john67 |
last post by:
The company I work for is about to embark on developing a commercial
application that will cost us tens-of-millions to develop. When all is
said and done it will have thousands of business...
|
by: Paul Cochrane |
last post by:
Hi all,
I've got an application that I'm writing that autogenerates python code
which I then execute with exec(). I know that this is not the best way to
run things, and I'm not 100% sure as to...
|
by: Shi Mu |
last post by:
Got confused by the following code:
>>> a
>>> b
>>> c
{1: , ], 2: ]}
>>> c.append(b.sort())
>>> c
{1: , ], 2: , None]}
|
by: mystilleef |
last post by:
Hello,
What is the Pythonic way of implementing getters and setters. I've
heard
people say the use of accessors is not Pythonic. But why? And what is
the alternative? I refrain from using them...
|
by: metaperl |
last post by:
A Comparison of Python Class Objects and Init Files for Program
Configuration
=============================================================================
Terrence Brannon
bauhaus@metaperl.com...
|
by: WaterWalk |
last post by:
I've just read an article "Building Robust System" by Gerald Jay
Sussman. The article is here:
http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf
In it there is a...
|
by: Jordan |
last post by:
Hi everyone,
I'm a big Python fan who used to be involved semi regularly in
comp.lang.python (lots of lurking, occasional posting) but kind of
trailed off a bit. I just wrote a frustration...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |