Strange compiler warning

Steve Holden sholden at holdenweb.com
Tue Oct 8 08:06:38 EDT 2002


"Robin Becker" <robin at jessikat.fsnet.co.uk> wrote in message
news:KdQzQgAUSpo9Ewch at jessikat.fsnet.co.uk...
> In article <3da227db$1 at news.sentex.net>, Peter Hansen
> <peter at engcorp.com> writes
> >Robin Becker wrote:
> .......
> >Basically:
> >
> >1. the "global" statement applies for the following scope of the
> >function, so you need only one,
> >
> >2. you can check if it exists simply by trying to reference it,
> >
> >3. using exceptions is generally better than not.
> >
> >But what are you really trying to accomplish?
> >
> >-Peter
> >
> that will do. I keep thinking that global brings something into scope,
> but in fact it's only declarative and doesn't have to exist already.
>
> As for the try except is it really true that
>
> try:
>     _dbg
> except:
>     _dbg = .....
>
> is better than
>
> if not globals().has_key('_dbg'):
>     _dbg = .....
>
Who's to say which is better? They are certainly *different*, since the
former will pick up _dbg from any of the usual suspects' scope, whereas the
latter explicitly tests just one single namespace.

When _dbg is declared global (sorry, I was picking nits) then it's perhaps a
matter of taste (<gasp>, you mean there really *is* more than one way to do
it?). Who's to say which is faster? Only a timing test will tell you. Was
speed your criterion for "better"? Even if it's readability, ut's still
pretty much a matter of taste.

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list