Hrvoje Niksic wrote:
> import os
> from gtk import *
> 
> class Foo (GtkWindow):
>     home = os.environ["HOME"]
> 
>     def __init__ (self):
>         GtkWindow.__init__ (self)
> 
>     def changehome (self, newhome):
>         self.home = newhome
> 
> foo = Foo ()
> print foo.home
> foo.changehome ("/")
> print foo.home

Ok, the problem here is that when we assign to self.home, our
__setattr__ function sets home as a shared attribute, since 'home'
doesn't exist in the instance dictionary.  However, the instance's
__getattr__ method is *NEVER* called, so we don't get a chance to return
the "updated" value.

class Bar:
  home = os.environ["HOME"]
  def __init__(self):
    pass

  def changehome(self,newhome):
    self.home = newhome

  def __getattr__(self,attr):
    print "__getattr__: %s" % attr
    return self.__dict__[attr]

  def __setattr__(self,attr,value):
    print "__setattr__: %s, %s" % (attr,str(value))
    self.__dict__[attr] = value

bar1,bar2 = Bar(),Bar()
print bar1.home,bar2.home
bar1.changehome ("/tmp")
print bar1.home,bar2.home

When you run the above, you get the following output (or something
similar!):

/root /root
__setattr__: home, /tmp
/tmp /root

Significant things to notice are that assigning to home creates an
instance attribute, so that the change isn't reflected in bar2.  Also,
notice that __getattr__ is *NEVER* called.

I think we can finally finish this by assigning to the shared attribute
if '_o' is defined, but *ALWAYS* setting the non-shared attribute.  With
this technique, __getattr__ should only be called from signal handlers,
where the instance won't contain the attribute, so we need to fetch it
from the shared dictionary.

I'll post a patch to test this shortly (I want to get the current
snapshot first).

-- 
Richard Fish                      Enhanced Software Technologies, Inc.
Software Developer                4014 E Broadway Rd Suite 405
[EMAIL PROTECTED]                    Phoenix, AZ  85040 
(602) 470-1115                    http://www.estinc.com
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]

Reply via email to