Re: What's the life time of the variable defined in a class function?



On Apr 30, 5:20 pm, "Diez B. Roggisch" <d...@xxxxxxxxxxxxx> wrote:
人言落日是天涯,望极天涯不见家 wrote:
Please see the followed example:
class A:
    def __init__(self):
        pass

class X:
    def __init__(self):
        n = 200
        if True:
            j = 200
        m = j
        k = A()
        print m, j

a = X()
# ?? what about the m, n and j? is it still alive?
del a

--------------------------
In C/C++, the life time of m,n and j was the nearest block.  but
obviously, python doen't have this syntax, but I would like to know
that whether the life time of  m, n, j  is base on function range or
the object range.

We can not access the m, n, and j from the outside of class X. Now I'm
writing a program base on the wxpython. In the __init__ function of
wx.Panel, I use normal varable(just like the m,n and j) created some
widgets. It could be show in the window.  Does it indicated the life
time of varable m,n,j is base on the object range?

Python has no variables. It has objects, which can be bound to names. Each
binding to a name will increase a reference counter. Each unbinding will
decrease it. so

a = SomeObject()
b = a
del a

will result in the SomeObject-instance still be alive. But when you add

del b

it will be garbage collected.

Now in your example A() bound to k will not survive the exit of the method,
as that means that k goes out of scope, and the object is bound to - the
A-instance - gets its reference-counter decreased, resulting in it being
freed.

The wxwidgets example though is a different thing. If the panel stores a
reference to the object, e.g. via a list (being part of a list or dict also
increases the reference count), it will be kept around.

Diez- Hide quoted text -

- Show quoted text -
Yes, I see. Many thanks for you !

.



Relevant Pages

  • Re: Whats the life time of the variable defined in a class function?
    ... In C/C++, the life time of m,n and j was the nearest block. ... time of varable m,n,j is base on the object range? ... binding to a name will increase a reference counter. ...
    (comp.lang.python)
  • Re: Question about idioms for clearing a list
    ... immediate colleagues). ... alone that it is fundamental to Python, and I have no idea what ... it's like lego bricks. ... forgotten about del, and that you don't immediately think about slices ...
    (comp.lang.python)
  • Re: Unexpected Behavior Iterating over a Mutating Object
    ... In Python 2.4 you can do: ... I'm not sure why 'DEL' is at the end of all the strings ... iterating over an item that is mutating seems like a Bad ...
    (comp.lang.python)
  • Re: Programmare per il web...
    ... questo che lo rende il nemico naturale del Perl :-) ... questa linea-guida del linguaggio Python coincide con ... Il punto 4 coincide col punto dello "Zen of Python" citato da Lawrence, ... I punti 1 e 2 sono abbastanza congruenti anche a Perl (ma non a Java o ...
    (it.comp.macintosh)
  • Re: class that keeps track of instances
    ... Have you tried the Python Cookbook? ... instance doesn't disappear when I think it should disappear. ... py> del c ...
    (comp.lang.python)