Local variables in classes and class instantiation

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()
 
P

Peter Otten

A.J. Bonnema said:
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
 
G

Gabriel Genellina

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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top