del behavior

E

Eric Snow

I was reading in the documentation about __del__ and have a couple of
questions. Here is what I was looking at:

http://docs.python.org/reference/datamodel.html#object.__del__

What is globals referring to in the following text from that reference
page?

"Starting with version 1.5, Python guarantees that globals whose name
begins with a single underscore are deleted from their module before
other globals are deleted; if no other references to such globals
exist, this may help in assuring that imported modules are still
available at the time when the __del__() method is called."

Thus those with an _ get deleted before everything else. This is not
referring to members of my objects is it, such that those members
starting with _ get deleted first? I suppose that would delete
__del__ before it would get called so I assume that is not the case.
But I want to be sure about that behavior and exactly what globals
is. Is globals meaning the contents of "globals" or something else.
I ask because sometimes some words get used for varied meanings.
 
C

Chris Rebert

I was reading in the documentation about __del__ and have a couple of
questions. Here is what I was looking at:

http://docs.python.org/reference/datamodel.html#object.__del__

What is globals referring to in the following text from that reference
page?

Globals are variables that have toplevel module scope. Basically, any
assignments, function definitions, or class definitions with no
indentation from the left margin will create a global variable. If you
can get at the variable by appending something of the form
"\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
function, then it's a global.

Cheers,
Chris
 
E

Eric Snow

Globals are variables that have toplevel module scope. Basically, any
assignments, function definitions, or class definitions with no
indentation from the left margin will create a global variable. If you
can get at the variable by appending something of the form
"\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
function, then it's a global.

Cheers,
Chris

Perfect! that is kind of what I thought. Thanks.

So any such in any module every variable in memory that starts with an
underscore will be deleted before the rest. Then this does not affect
the order in which variables are deleted in instances of my classes,
and thus all my class and instance variables (including methods) are
available when the __del__ of the class instance is called?

-eric
 
E

Eric Snow

Perfect!  that is kind of what I thought.  Thanks.

So any such in any module every variable in memory that starts with an
underscore will be deleted before the rest.  Then this does not affect
the order in which variables are deleted in instances of my classes,
and thus all my class and instance variables (including methods) are
available when the __del__ of the class instance is called?

-eric

Typo.

Perfect! that is kind of what I thought. Thanks.

So in any module every such variable in memory that starts with an
underscore will be deleted before the rest. Then this does not affect
the order in which variables are deleted in instances of my classes,
and thus all my class and instance variables (including methods) are
available when the __del__ of the class instance is called?

-eric
 
C

Chris Rebert

Perfect! that is kind of what I thought. Thanks.

So any such in any module every variable in memory that starts with an
underscore will be deleted before the rest. Then this does not affect
the order in which variables are deleted in instances of my classes,
and thus all my class and instance variables (including methods) are
available when the __del__ of the class instance is called?

Indeed. The underscore special-casing only applies to modules.

Cheers,
Chris
 
M

MRAB

Chris said:
Globals are variables that have toplevel module scope. Basically, any
assignments, function definitions, or class definitions with no
indentation from the left margin will create a global variable. If you
can get at the variable by appending something of the form
"\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
function, then it's a global.
Actually, the amount of indentation doesn't matter. What matters is
whether it's within a 'def' or 'class' statement or not.
 
C

Chris Rebert

Actually, the amount of indentation doesn't matter. What matters is whether
it's within a 'def' or 'class' statement or not.

Yes, but those do require you *to indent* (though so do while & if for
that matter); I just couldn't seem to come up with a better
description of the rule at the time.
But you are correct and yours is a much better description of the rule.

Cheers,
Chris
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top