NB question on global/local variables in functions
Wolfgang
wollez at gmx.net
Sat Jul 15 08:01:03 EDT 2006
>
> First, avoid "from function import *" as it pollutes your namespace. Either
> import specific symbols or just the module:
>
> from function import fun, fun1
> import function
thanks for the hint! But what is the difference between
from module import *
and
import module
?
>
> Second, if you really must, add an __all__ list to function.py, generally
> right at the top:
>
> __all__ = ['fun', 'fun1']
I've read this already, but to keep the example short I've skipped that
part.
>
> (Note that __all__ is a list of strings.) The "from star" import will only
> import the names in the __all__ list. You can also "hide" individual names
> from a "from star" import by prefixing them with a single underscore (e.g.,
> "_c1" instead of "c1").
>
the _ before the variable did the trick! Thanks a lot!
> Third, one of Python's key concepts is "we're all adults here". You can't
> really and truly hide c1 and c2, so just act responsibly and don't mess with
> them from outside the function module.
It is more to prevent myself from my own stupidness by changing global
variables incidentally.
Wolfgang
More information about the Python-list
mailing list