confused by bindings

Chris Barker chrishbarker at home.net
Wed Sep 19 19:16:27 EDT 2001


Sam Falkner wrote:

> I might have thought this too, but look at the wonderful Borg recipe:
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
> 
> Aren't they doing exactly what I was doing?
>     self.__dict__ = self.__shared_state
 
Nope. what is happening here is that the instance variable self.__dict__
is being set to refer to the the class variable self.__shared_state.
This would be the same as if you did, in your example:

class Cell:
    count = 0
    def __init__(self, stuff):
        self.local_count = self.count + 1
        print 'count is', self.count

however, that wouldn't be useful, as count is immutable, so the Class
variable, count would never get changed. What I suggested in an earlier
post is similar to this borg pattern:

A) You can use an immutable type for count, so that you can change it's
contents, rather than creating a new one:

class Cell:
    count = [0]
    def __init__(self):
        self.count[0] += 1
        print 'count is', self.count


Now Cell.count and self.count both refer to the same mutable object, and
thus changes are seen everywhere.


Of course, the whole point of the borg pattern is to replicate the
ENTIRE class, so you make sure that all instances have the same
.__dict__. That's a special case, and may or may not be what you want.


> re-inserting my test case:
> 
> --- file test.py ---
> 
> class CellTestCase(unittest.TestCase):
>     def testforleaks(self):
>         data.Cell.count = 0
>         # do a whole bunch of stuff
>         # x = a gnarly collection of Cell objects
>         # count number of Cell objects in x
>         self.failUnlessEqual(count-of-Cells-from-x, data.Cell.count)
> 
> You can see that I reset the counter, via data.Cell.count = 0, to
> clean up from the other test cases.  As I mentioned above, my test now
> succeeds when I try it your way, but not if I run the whole test suite.

Again, post some runnable code that exhibits your problem, it will be a
whole lot easier for us to figure out what is going on, and you may even
find your problem when you try to narrow it down.

Also, unless you decrease Cell.count when you delete an instance, I
can't see how this would be useful.

The name of this function implies that you are testing for memory leaks.
Python's refence counting scheme makes it pretty hard to have such
things. The only time they show up is with circular references, and I
think newer versions of Python even clean those up, so what are you
really trying to do here?

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list