__setattr__ and acquisition ( was RE: __getattr__ and acquisition)
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
Nicholas Henke (by way of Nicholas Henke ) 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') ?
Acquisition uses a subtle trick that makes everything work: the "self" passed to most methods is actually an acquisition wrapper, rather than the object itself. But the "self" passed to certain methods like __setattr__() is not wrapped. I'm sure there's a good reason. HTH. Shane
Yep. This is a problem for me I'm trying to find something through acquisition in my __setattr__. Self is totally unwrapped. Can anyone think of a creative solution? aq_acquire doesn't help because self is not a wrapped object. :( Can't pass in the object I want because the whole point of the setattr is to get around the function call. -EAD Shane Hathaway wrote:
Nicholas Henke (by way of Nicholas Henke ) 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') ?
Acquisition uses a subtle trick that makes everything work: the "self" passed to most methods is actually an acquisition wrapper, rather than the object itself. But the "self" passed to certain methods like __setattr__() is not wrapped. I'm sure there's a good reason.
HTH.
Shane
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Why rely on __setattr__? Why not just create a regular setter function that can use acquisition instead of being clever? -Casey On Wed, 2002-06-05 at 15:24, Erik A. Dahl wrote:
Yep. This is a problem for me I'm trying to find something through acquisition in my __setattr__. Self is totally unwrapped. Can anyone think of a creative solution? aq_acquire doesn't help because self is not a wrapped object. :( Can't pass in the object I want because the whole point of the setattr is to get around the function call.
-EAD
Shane Hathaway wrote:
Nicholas Henke (by way of Nicholas Henke ) 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') ?
Acquisition uses a subtle trick that makes everything work: the "self" passed to most methods is actually an acquisition wrapper, rather than the object itself. But the "self" passed to certain methods like __setattr__() is not wrapped. I'm sure there's a good reason.
HTH.
Shane
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
The only way I know is to put a wrapped object in a ._v_attribute, which means a volatile attribute. You can put, for instance, a wrapped self in, for instance self._v_alterEgo, this way you can do wrapped transversals thru it. How do you get a wrapped self to put there is an exercise left to the reader :-) tip: it's no use trying to do it in .__setstate__, as self is not wrapped there either, last I checked. On Wed, 2002-06-05 at 16:24, Erik A. Dahl wrote:
Yep. This is a problem for me I'm trying to find something through acquisition in my __setattr__. Self is totally unwrapped. Can anyone think of a creative solution? aq_acquire doesn't help because self is not a wrapped object. :( Can't pass in the object I want because the whole point of the setattr is to get around the function call.
-- Ideas don't stay in some minds very long because they don't like solitary confinement.
AFAIK acquisition bows out quietly during __getattr__, __setattr__. You'll have to find another way to pass in the "thing" values before __setattr__ gets called. Maybe try an explicit self.aq_acquire('thing'). Maybe that would work. -Casey On Wed, 2002-06-05 at 09:33, Nicholas Henke 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
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/>
On Friday 07 June 2002 06:44 pm, Julien Jalon wrote: Beautiful -- thanks a ton ~~ nic
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/>
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
participants (7)
-
Casey Duncan -
Erik A. Dahl -
Julien Jalon -
Leonardo Rochael Almeida -
Nicholas Henke -
Nicholas Henke -
Shane Hathaway