Circular reference problem -- advice?

Huaiyu Zhu hzhu at localhost.localdomain
Tue Jul 11 14:26:27 EDT 2000


On Mon, 10 Jul 2000 22:20:24 -0700, Erik Max Francis <max at alcyone.com> wrote:
>shindich at my-deja.com wrote:
>
>> It seems to me that there is no need to have this map at all.
>
>It actually is necessary for my purposes, since the strings that is used
>for dispatching will not be a status code (a three digit number as a
>string), not actually the name of the method to be invoked.  (I simply
>showed it that way for the purposes of the sample code; certainly in the
>example I showed you are correct.)
>

In this case you can still have the map, but it maps to strings instead of
methods, and use getattr to get the actual methods.  No circular reference.

#!/usr/bin/env python
class C:
    def __init__(self):
        print "in constructor"
        self.map = {'111': 'f', '222': 'g'}

    def __del__(self):
        print "in destructor"

    def f(self):
        print "in C.f"

    def g(self):
        print "in C.g"

    def dispatch(self, name):
        methodname = self.map[name]
        func = getattr (self, methodname)
        apply (func, [])

c = C()
c.dispatch('111')



More information about the Python-list mailing list