473,498 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Apr 30 '07 #1
2 1260
人言落日是天涯,望极天涯不见家 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
Apr 30 '07 #2
On Apr 30, 5:20*pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
人言落日是天涯,望极天涯不见家 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 !

Apr 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
6497
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
56
3679
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
121
9898
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
100
5156
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
13
4996
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
24
3308
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
669
25375
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
18
1817
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) {...
5
1957
by: Vols | last post by:
class A{ public: int x; }; class B : public A{ public: int y; }; void foo()
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.