[Zope-dev] Overriding __getattr__ for requests only.

Jim Fulton jim@digicool.com
Mon, 22 Nov 1999 09:07:46 -0500


"Jeff K. Hoffman" wrote:
> 
> Hello, everyone.
> 
> I am trying to write a new subclass of OFS.Folder (as a python product) to
> do the following: When a user requests /myFolder/index_html, myFolder
> will search itself, then its list of 'parents', returning the attribute
> from one of them if they have it, and raising an AttributeError if not.
> 
> I have had some moderate success by overloading __getattr__ as follows:
> 
>   def __getattr__(self, name):
>       try:
>           return OFS.Folder.Folder.__getattr__(self, name)
>       except:
>           for parent in self.parentList:
>               try:
>                   return getattr(parent, name)
>               except AttributeError:
>                   pass
>           raise AttributeError, name

Never override __getattr__ on persistent objects.  __getattr__ is
already overridden by the persistence machinery and overriding it 
yourself will cause big problems.

You can accomplish the same thing by overriding __getitem__, being careful
to include (or call) the base __getitem__ inherited from ObjectManager.

When publishing a URL, Zope tries __bobo_traverse__, then __getattr__, 
then __getitem__.

If you *don't* want to acquire, you will also need to mix-in 
Acquisition.Explicit, before Folder (or ObjectManager).

(snip)

Jim

--
Jim Fulton           mailto:jim@digicool.com   Python Powered!        
Technical Director   (888) 344-4332            http://www.python.org  
Digital Creations    http://www.digicool.com   http://www.zope.org    

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.