473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Local variables in classes and class instantiation

Hi all,

I just started using Python. I used to do some Java programming, so I am
not completely blank.

I have a small question about how classes get instantiated within other
classes. I have added the source of a test program to the bottom of this
mail, that contains 3 methods within a testclass that each instantiate
the same class and bind it to a local variable. My understanding was,
that the local variable gets garbage collected as soon as the method
goes out of scope. Thus I would expect the local variable 'cpu' in these
methods to be independant and create different instances of the class CPU.

Still, when I execute all three methods, I get two instances that are
equal and the third is different.
Is there some circomstance that makes two object creations result in the
same object?

================================================== =========== output
Output from the (test)program is:
cpu class = <cpu.CPU instance at 0x8244eec>
..cpu class = <cpu.CPU instance at 0x8244eec>
..cpu class = <cpu.CPU instance at 0x8244f0c>
..
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
================================================== ============= source
The source of the test program is:

import sys
import unittest
from cpu import CPU

class cpuAddEntries(unittest.TestCase):
def testEmptyCPU(self):
"Test empty CPU."
expected="{}"
cpu = CPU("cpu01")
print "cpu class = "+repr(cpu)
result = cpu.showTimes()
self.assertEquals(expected,result)
def testOneEntry(self):
"Test one entry into CPU"
time = "0000"
expected="{'%s': ('user', 'system')}" % time
cpu = CPU("cpu02")
print "cpu class = "+repr(cpu)
cpu.addMetric (time, "user", "system")
result = cpu.showTimes()
self.assertEquals(expected,result)
def testDuplicate(self):
"Test inserting a duplicate entry."
global exceptions
time = "0000"
expected="{'%s': ('user', 'system')}" % time
cpu = CPU("cpu03")
print "cpu class = "+repr(cpu)
cpu.addMetric (time, "user", "system")
self.assertRaises(Exception, cpu.addMetric, time, "user1",
"system1")

if __name__ == "__main__":
unittest.main()

--
A.J. Bonnema, Leiden The Netherlands,
user #328198 (Linux Counter http://counter.li.org)

Dec 23 '07 #1
2 1975
A.J. Bonnema wrote:
I have a small question about how classes get instantiated within other
classes. I have added the source of a test program to the bottom of this
mail, that contains 3 methods within a testclass that each instantiate
the same class and bind it to a local variable. My understanding was,
that the local variable gets garbage collected as soon as the method
goes out of scope. Thus I would expect the local variable 'cpu' in these
methods to be independant and create different instances of the class CPU.

Still, when I execute all three methods, I get two instances that are
equal and the third is different.
Is there some circomstance that makes two object creations result in the
same object?

================================================== =========== output
Output from the (test)program is:
cpu class = <cpu.CPU instance at 0x8244eec>
.cpu class = <cpu.CPU instance at 0x8244eec>
.cpu class = <cpu.CPU instance at 0x8244f0c>
.
That two instances of CPU print the same "at 0x..." representation doesn't
mean they are the same object, they may just be located at the same
location in memory. For that to happen it is neccessary (but not
sufficient) for the first instance to be garbage-collected.

Peter
Dec 23 '07 #2
En Sun, 23 Dec 2007 08:05:18 -0300, Peter Otten <__*******@web.de>
escribió:
>Still, when I execute all three methods, I get two instances that are
equal and the third is different.
Is there some circomstance that makes two object creations result in the
same object?

================================================= ============ output
Output from the (test)program is:
cpu class = <cpu.CPU instance at 0x8244eec>
.cpu class = <cpu.CPU instance at 0x8244eec>
.cpu class = <cpu.CPU instance at 0x8244f0c>
.

That two instances of CPU print the same "at 0x..." representation
doesn't
mean they are the same object, they may just be located at the same
location in memory. For that to happen it is neccessary (but not
sufficient) for the first instance to be garbage-collected.
One way to avoid such ambiguity is to ensure that all three objects are
alive at the same time; by example, inserting them into some global list.

--
Gabriel Genellina

Dec 27 '07 #3

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

Similar topics

3
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static...
2
by: srijit | last post by:
Hello Members, I do not see any direct mention of class variables in new style classes. Only class methods. Have I missed or is it trivial? If not, how to define/implement class variables for new...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
0
by: Peter Vestergaard | last post by:
Hi, I am running VS .Net 2005. I have an application in which one of the classes are having a member that is an instance of a class defined in a managed C++ dll. As soon as I try to create an...
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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 project—planning, coding, testing,...

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.