Newbie asks about static variables...

Martijn Faassen faassen at pop.vet.uu.nl
Thu May 6 04:06:33 EDT 1999


William Tanksley wrote:
> 
> On Tue, 04 May 1999 20:44:16 +0200, Martijn Faassen wrote:
> 
> >Well, using a global variable would indeed be one way. One need not be
> >as worried with global variables in Python, as there's a nice module
> >system that puts them in their own namespaces. If you prepend your
> >global variable with a _ then it doesn't get imported, even if you use
> >'from <module> import *'.
> 
> You know, I've heard of a lot of very knowledgable people say that _ is
> the prefix for hiding names, but when I tried it it didn't work.
> 
> Looking in the manual, I see that it claims that __ is the correct prefix;
> and indeed, this seems to work.
> 
> So...  What am I missing?

(comes back from some actual testing)

There's a combination of your missing something and me missing something
too:

_ is used to hide module level global variables from imports, but not
from all imports, it turns out

__ is used to make data members of a class private (by mangling the
namespace)

However, my testing indicates that the '_' behavior wasn't as I thought
it would be. A global variable _foo indeed isn't imported if you do a
'from module import *'. But it's perfectly possible to do 'import
module; print module._foo', it turns out to my surprise! This seems to
me rather inconsistent behavior; is there a reason why this is so?

Of-course-I-never-noticed-this-anyway-as-I-just-never-tried-before-ly
yours,

Martijn




More information about the Python-list mailing list