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

G

Guest

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?

Sorry for my poor english!
It seems
 
D

Diez B. Roggisch

人言è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ said:
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
 
G

Guest

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 !
 

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,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top