confused by bindings

Christian Tanzer tanzer at swing.co.at
Fri Sep 21 02:45:07 EDT 2001


> class Borg:
>     __shared_state = {}
>     def __init__(self):
>         self.__dict__ = self.__shared_state
>         self.countone = 0
> 
> class One:
>     def __init__(self):
>         borg = Borg()
>         borg.countone += 1

Your code resets `countone` to zero every time a new Borg is created.
Try this instead:

class Borg:
    __shared_state = {"countone" : 0}
    def __init__(self):
        self.__dict__ = self.__shared_state

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list