del behavior

Discussion in 'Python' started by Eric Snow, Jan 7, 2009.

  1. Eric Snow

    Eric Snow Guest

    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.
     
    Eric Snow, Jan 7, 2009
    #1
    1. Advertising

  2. Eric Snow

    Chris Rebert Guest

    On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow <> wrote:
    > 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

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Jan 7, 2009
    #2
    1. Advertising

  3. Eric Snow

    Eric Snow Guest

    On Jan 7, 12:48 pm, "Chris Rebert" <> wrote:
    > On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow <> wrote:
    > > 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
    >
    > --
    > Follow the path of the Iguana...http://rebertia.com


    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
     
    Eric Snow, Jan 7, 2009
    #3
  4. Eric Snow

    Eric Snow Guest

    On Jan 7, 12:55 pm, Eric Snow <> wrote:
    > On Jan 7, 12:48 pm, "Chris Rebert" <> wrote:
    >
    >
    >
    > > On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow <> wrote:
    > > > 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

    >
    > > --
    > > Follow the path of the Iguana...http://rebertia.com

    >
    > 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
     
    Eric Snow, Jan 7, 2009
    #4
  5. Eric Snow

    Chris Rebert Guest

    On Wed, Jan 7, 2009 at 11:55 AM, Eric Snow <> wrote:
    > On Jan 7, 12:48 pm, "Chris Rebert" <> wrote:
    >> On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow <> wrote:
    >> > 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
    >>
    >> --
    >> Follow the path of the Iguana...http://rebertia.com

    >
    > 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

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Jan 7, 2009
    #5
  6. Eric Snow

    MRAB Guest

    Chris Rebert wrote:
    > On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow <> wrote:
    >> 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.
    >

    Actually, the amount of indentation doesn't matter. What matters is
    whether it's within a 'def' or 'class' statement or not.
     
    MRAB, Jan 7, 2009
    #6
  7. Eric Snow

    Chris Rebert Guest

    On Wed, Jan 7, 2009 at 12:05 PM, MRAB <> wrote:
    > Chris Rebert wrote:
    >>
    >> On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow <> wrote:
    >>>
    >>> 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.
    >>

    > 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

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Jan 7, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    353
  2. Replies:
    0
    Views:
    312
  3. Pedro Graca

    avoiding CTRL-ALT-DEL by mistake

    Pedro Graca, Jan 11, 2004, in forum: C Programming
    Replies:
    1
    Views:
    376
    Kevin Goodsell
    Jan 11, 2004
  4. Raymond Arthur St. Marie II of III

    Del's "except"ional PEP Rejection

    Raymond Arthur St. Marie II of III, Jul 27, 2003, in forum: Python
    Replies:
    0
    Views:
    334
    Raymond Arthur St. Marie II of III
    Jul 27, 2003
  5. Eric Snow

    del behavior 2

    Eric Snow, Jan 7, 2009, in forum: Python
    Replies:
    9
    Views:
    280
    Eric Snow
    Jan 8, 2009
Loading...

Share This Page