Use and usefulness of the as syntax

alex23 wuwei23 at gmail.com
Thu Nov 17 21:38:47 EST 2011


On Nov 18, 1:48 am, candide <cand... at free.invalid> wrote:
> # a.py
> import math as _math
>
> # b.py
> from a import *
>
> print _math.sin(0)       # raise a NameError
> print math.sin(0)        # raise a NameError
>
> so the as syntax is also seful for hiding name, isn'it ?

Not exactly. It's the * import mechanism here that's ignoring any
bindings that begin with an underscore. If you had:

   _dummy = 1

...inside of a.py, it wouldn't be pulled in either. As you state
later, 'as' is purely a binding convenience.

Incidentally, you can still allow * import to bring in underscore-
prefixed bindings by adding them to an __all__:

    __all__ = ['_dummy']



More information about the Python-list mailing list