C-style static variables in Python?

Ethan Furman ethan at stoneleaf.us
Fri Apr 2 16:33:48 EDT 2010

Patrick Maupin wrote:


> Well, I think the whole discussion has basically been about personal
> preference.  OTOH, but if you call the function a few million times,
> you might find the cost of try/except to be something that you would
> rather not incur -- it might become a performance issue rather than a
> personal choice issue.  On the other OTHER hand, if you call the
> function a few billion times, performance weighs more heavily in favor
> of the closure approach rather than the object approach, since local
> variable lookup is so much cheaper.

Indeed.  I was surprised to find your __getattr__ approach faster than 
the try/except approach (about 20% on my machine).  I'll have to think 
about that for future situations like this.

My main point, though, was using __call__, and not some weird _ method.  ;)


More information about the Python-list mailing list