Local variables in classes and class instantiation

Discussion in 'Python' started by A.J. Bonnema, Dec 23, 2007.

  1. A.J. Bonnema

    A.J. Bonnema Guest

    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)
     
    A.J. Bonnema, Dec 23, 2007
    #1
    1. Advertising

  2. A.J. Bonnema

    Peter Otten Guest

    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
     
    Peter Otten, Dec 23, 2007
    #2
    1. Advertising

  3. En Sun, 23 Dec 2007 08:05:18 -0300, Peter Otten <>
    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
     
    Gabriel Genellina, Dec 27, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page