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

D

Daniel Mark

Hello all:

I am looking the sample code posted on PIL website
http://www.pythonware.com/library/pil/handbook/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
 
J

John Bokma

Daniel Mark said:
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
 
S

Steve Holden

Daniel said:
Hello all:

I am looking the sample code posted on PIL website
http://www.pythonware.com/library/pil/handbook/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
 
F

Fredrik Lundh

Steve said:
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>
 

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
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top