[Zope-dev] __setattr__ and acquisition ( was RE: __getattr__ and acquisition)

Julien Jalon jj@nuxeo.com
Fri, 7 Jun 2002 22:44:55 +0000 (UTC)


Nicholas Henke  <henken@unholymess.com> wrote:
>Given the following code:
> I can see why access to self.thing fails in Inner::__setattr__, but the 
>question is how do I do that -- can I not use __setattr__ and have to use a 
>setAttr that is accessed via O.I.setAttr('help','me rhonda') ?
>
>Nic
>
>import ExtensionClass, Acquisition
>
>class Outer(ExtensionClass.Base):
>	thing = ('help','donthelp')
>
>class Inner(Acquisition.Implicit):
>	def __setattr__(self,name,value):
>		if name in self.thing:
>			self.__dict__['name'] = value
>		else:
>			print "Bad attribute"
>			
>O = Outer()
>I = Inner()
>O.I = I
>print O.I.thing  # is ok  --> gives ('help','donthelp')
>O.I.help = 'me rhonda' # AttributeError: thing


import ExtensionClass
import Acquisition

class Outer(ExtensionClass.Base):

    thing = ('help', 'donthelp')

class Inner_base(Acquisition.Implicit):
    # here the real declaration of the class

    def aMethod(self):
        print self.help

class Inner(Inner_base):
    # here the class to use

    def __of__(self, parent):
        real_object = Inner_wrapper()
        real_object.__dict__['_parent'] = parent
        real_object.__dict__['_inner'] = self
        return real_object.__of__(parent) 

class Inner_wrapper(Inner_base):

    def __setattr__(self,name,value):
        if name in self.__of__(self._parent).thing:
            self.__dict__['_inner'].__dict__[name] = value
        else:
            print "Bad attribute"

    def __getattr__(self,name):
        return getattr(self.__dict__['_inner'], name)

O = Outer()
I = Inner()
O.I = I

O.I.help = 'me rhonda'
O.I.aMethod()
I.aMethod()
print I.__dict__
print O.I.__dict__
O.I.badattr = 'blah'

-- 
Julien Jalon
<http://nuxeo.com/>