C-style static variables in Python?

Patrick Maupin pmaupin at gmail.com
Fri Apr 2 20:25:09 EDT 2010

On Apr 2, 6:57 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 02 Apr 2010 12:39:16 -0700, Patrick Maupin wrote:
> > On Apr 2, 2:38 pm, Ethan Furman <et... at stoneleaf.us> wrote:
> [...]
> >> Sounds like a personal preference issue, rather than a necessary /
> >> unnecessary issue -- after all, if you call that function a thousand
> >> times, only once is mongo not defined... clearly the exception.  ;)
> >> ~Ethan~
> > 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.
> The cost of a try...except is *very* low -- about the same as a pass
> statement:

Agreed.  In the example above, if frobnicate() is a null function, the
try/except adds about 5% to execution time on my machine.  If I were
really worried about execution time, I would use a closure *for this
particular example* as I mentioned elsewhere.  However, the cost of
the try/except is not zero, and when I have something I prefer looking
at (the __getattr__ doesn't clutter up the main-line execution with
conditionals for stuff that only gets used once at initialization)
that is always known to be cheaper in execution, that's what I use.  I
suppose some people might not like looking at the __getattr__, but
this is a memoization technique I use quite often, so I find it


More information about the Python-list mailing list