re compiled object result caching?

Steve Holden steve at holdenweb.com
Wed Aug 29 18:20:19 EDT 2007


Dan Stromberg - Datallegro wrote:
> On Wed, 29 Aug 2007 17:45:36 -0400, Steve Holden wrote:
> 
>> Dan wrote:
>>> foo_re = re.compile(r"foo(bar)")
>>> # . . .
>>> if foo_re.search(line):
>>>     foo_re.last_result().group(1)
>>>
>> If you wanted to implement this I don't really see why a method call is 
>> necessary. It would surely only need to be a simple attribute?
>>
>> Of course you then introduce the possibility that someone will reference 
>> if before using the RE in a search, plus it still requires separate 
>> storage if you want the ability to use the same RE for two different 
>> matches and compare the match results.
> 
> I've long felt that publicly accessible attributes probably should be
> syntactic sugared to look like accessor methods, a bit like how __add__
> ends up being + - so that if your attribute ever needs to become methods
> (say, it started out life as a unidimensional thing, but later needs to
> be a flattening of 3 dimensions or something), you won't necessarily need
> to change depenent code.
> 
> But are methods a lot more expensive in python than referencing other
> kinds of attributes?
> 
Well, given that they are procedure calls, yes. There's also other magic 
over and above that.

The property() function is a way of making what appear to be attribute 
accesses become method calls. Your intuition is taking you the wrong 
way: if you need an accessor method then just turn the normal attribute 
into a property. Given that mechanism, your wish to use accessor methods 
is definitely anti-pythonic.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list