Updating file objects automatically

Craig Ringer craig at postnewspapers.com.au
Thu Dec 30 03:27:30 EST 2004


On Thu, 2004-12-30 at 11:15, Jorge Luiz Godoy Filho wrote:

> Is there something I can do to change all instances of the Search class? 
> Are there different alternatives for such a "thing"?

I couldn't really catch your explanation, but mention of changing all
instances of a class suggests that you may be in a situation where you
need to modify the class, not its instances. There are two methods I use
when I have to change things across all instances:

def A(object):
    "A class that provides a variable shared by all instances,
    and a method of changing it using a normal method and, for
    example's sake, a class method."""

    class_level_variable = True

    def __init__(self):
        pass

    def getvalue(self):
        return self.class_level_variable

    def setvalue(self, newval):
        self.__class__.class_level_variable = newval

    def setvaluecls(cls, newval):
        cls.class_level_variable = newval
    setvaluecls = classmethod(setvaluecls)

sevaluecls and setvalue look the same to callers calling them on an
instance of the class.

I have no idea if that's actually appropriate for your needs, it's just
a stab in the dark, but perhaps it might be.

--
Craig Ringer




More information about the Python-list mailing list