Accessing a method from within its own code

Dave Angel davea at ieee.org
Mon Nov 2 17:57:35 CET 2009


Paddy O'Loughlin wrote:
>> I suspect that the "inspection" module has your answer, but that it'll be
>> bulkier, and much slower than just doing what you're doing already.
>>
>>     
> Hmm.
> Yeah, it does appear to be bulky. I don't think it's really any more use
> than what I'm doing already.
>
>
> Why not use the default arguments gimmick?  Since this cached item is to
>   
>> have a module lifetime, it'd be desirable to create it when the method is
>> being defined, which is exactly what default arguments do.
>>
>>     
>
> I've a few different ways of emulating static function variables from C/Java
> and the function/method attribute one is easily the one that appeals most to
> my sensibilities.
> I find the default arguments gimmick to be a gimmick. It's co-opting a piece
> of functionality for something it doesn't seem like it was originally
> intended for and as a consequence is less readable (to my eyes).
> The only problem with the function/method way is that it gets rather verbose
> when you are dealing with a user-defined method and have to give the class
> name and method name and it's also a bit of a pain for moving code around
> when refactoring.
>
> You ever wish there was more to python scoping than just locals(), globals()
> and __builtins__? Like a method's class's scope too?
> That's where I am at with this.
>
>
>   
Back to your original approach - if you don't bind a new value to a 
class variable, it's okay to just reference it with self, rather than 
needing cls.   But if you forget, and bind a new value, then you'll end 
up with an instance attribute instead of reusing the class attribute.

How about (untested):

class ...
    cacheitems = []
   def find_line(self, lines):
       if not self.cacheitems:
           self.cacheitems.append(    re.compile.....   )
       for ...
           m = self.cacheitems[0]( line )


Just don't put self.cacheitems= xyzzy  inside a method.

Incidentally, if your cacheitems is an instance of a dummy class (with 
pass as a body), you could use named parameters instead of subscript.  
But then you're back to the three nodes you already thought was too 
verbose.  All you've gained is the relative ease of refactoring.

DaveA




More information about the Python-list mailing list