A
A.J. Bonnema
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()
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()