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 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
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 ! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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, ...)...
|
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...
|
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"?
|
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
| |
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...
|
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...
|
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, ...) {...
|
by: Vols |
last post by:
class A{
public:
int x;
};
class B : public A{
public:
int y;
};
void foo()
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |