[Zope3-Users] Persistent obj with attr hooks

Marius Gedminas marius at gedmin.as
Thu Jul 23 16:26:25 EDT 2009


On Thu, Jul 23, 2009 at 01:41:57PM -0400, Jim Pharis wrote:
> I have a class that inherits from Persistent but also needs to hook into
> setattr and getattribute.
> 
> class Test(Persistent)
>    ...
>   def __setattr__(self, name, val)
>   def __getattribute__(self, name)
> 
> Before when this class inherited from object the __setattr__ and
> __getattribute__ calls were simple and straight forward with
> object.__getattribute__(name). However, now that I inherent from Persistent,
> the equivalent Persistent.__getattribute__ and Persisent.__setattr__ don't
> seem to set/get my attributes.
> 
> Can someone help me out. What call should I be using instead?

Works For Me(TM):

    >>> from persistent import Persistent
    >>> class Test(Persistent):
    ...     def __setattr__(self, name, value):
    ...         print "setting %s to %r" % (name, value)
    ...         super(Test, self).__setattr__(name, value)
    ...     def __getattribute__(self, name):
    ...         print "getting %s" % (name)
    ...         return super(Test, self).__getattribute__(name)
    ...
    >>> apple = Test()
    >>> apple.color = 'green'
    setting color to 'green'
    >>> apple.color
    getting color
    'green'

Show us your code if you want more advice.

Marius Gedminas
-- 
IBM motto: "TEN vowels? Don't you know vowels are scrd?"
		-- Linus Torvalds
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20090723/a70aea8d/attachment.bin 


More information about the Zope3-users mailing list