missing methods or __getattr__ problem

Vincent Marchetti vincem at en.com
Fri Oct 4 06:26:52 EDT 2002


In article <upmdreshjh2u6b at corp.supernews.com>, "Uwe C. Schroeder"
<uwe at oss4u.com> wrote:

>  
> Hi, 
>  
> I'm a little stuck here and my brain turns in circles. 
> Here's what I'm trying to accomplish and I'm sure there is a very 
> simple solution to this problem: 
>  
> The caller calles a method xx.callme(objecttype, parameters) 
> The called object keeps a list of class instances where the 
> paricularly wanted object can be found via the "objecttype" 
> parameter. However the called object itself doesn't have the called 
> attribute. What I'm looking for is a way to transparently call a 
> method as if it was part of the called object. So the called object 
> has to do the following: 
> 1. figure out if the called attribute is a member of itself 
> 2. if not 1. take the first parameter and look up the object in 
> which the wanted method resides 
> 3. call the (hopefully found) method and return the results. 
>  


I think the solution here is to use a two step process in performing the call,
using an intermediate Python object to store the name of the method.
[Credit where credit is due -- I learned this pattern from the aetypes
module in the Mac Python distribution]

--- Example pseudo-python

class methodNameStorer:
   def __init__(self,name):
      self.myMethod = name
   
   def __call__(self, objToCall,arguments):  # see Python "Ref" Sec 3.3.4
      MethToCall = objToCall.__getattr__(self.myMethod)
      # now we have both the called object (objToCall) and arguments, can
      # do any logging desired
      MethToCall(arguments)   # finally!



#then, using naming from your example

class bng:
   def __getattr__(self, attr):
      if ({attr is a valid method name}):
         return methodNameStorer(attr)
      raise AttributeError

Vince Marchetti



More information about the Python-list mailing list