By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,914 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,914 IT Pros & Developers. It's quick & easy.

Do we need to delete "ImageDraw.Draw" after using it?

P: n/a
Hello all:

I am looking the sample code posted on PIL website
http://www.pythonware.com/library/pi.../imagedraw.htm

################################################
<<Draw a Grey Cross Over an Image>>

import Image, ImageDraw

im = Image.open("lena.pgm")

draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw # ===>>>Why we should delete this object draw?

# write to stdout
im.save(sys.stdout, "PNG")
#################################################

Is there any general rule that we must delete the object after using
it?
Thank you
-Daniel

Sep 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Daniel Mark" <da***********@gmail.comwrote:
Is there any general rule that we must delete the object after using
it?
In general: if the object is not destroyed when it goes out of scope (but
later) and uses precious resources [1], or if a special clean up is
required, you should delete it explicitly.
[1] like file handles

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Sep 20 '06 #2

P: n/a
Daniel Mark wrote:
Hello all:

I am looking the sample code posted on PIL website
http://www.pythonware.com/library/pi.../imagedraw.htm

################################################
<<Draw a Grey Cross Over an Image>>

import Image, ImageDraw

im = Image.open("lena.pgm")

draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw # ===>>>Why we should delete this object draw?

# write to stdout
im.save(sys.stdout, "PNG")
#################################################

Is there any general rule that we must delete the object after using
it?
Just consider it good hygiene.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 20 '06 #3

P: n/a
Steve Holden wrote:
>Is there any general rule that we must delete the object after using
it?
Just consider it good hygiene.
in this specific case, it may not be obvious for the casual reader that
the global "draw" variable will contain an indirect reference to the
original image object, so even if you throw away (or replace) the "im"
variable with something else, the original image will still be present
in memory.

if you put the same code inside a function, you can safely leave the
garbage handling to Python.

</F>

Sep 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.