How to avoid writing the name of the module all th

2019-06-22 07:37发布

问题:

I use the math module a lot lately. I don't want to write math.sqrt(x) and math.sin(x) all the time. I would like to shorten it and write sqrt(x) and sin(x). How?

回答1:

For longer module names it is common to shorten them, e.g.

import numpy as np

Then you can use the short name. Or you can import the specific stuff that you need, as shown in the other anwsers:

from math import sin, sqrt

This is often used inside packages, for code that is more closely coupled. For libraries the first option with the name shortening is often the prefered way.

What you should never do is use the from math import * form. It will pollute the name space, potentially leading to name collisions and making debugging harder. Most importantly it makes the code hard to read, because it is not clear where a particular function came from.

An exception can be made in the interactive interpreter. But once you are in the habit of using the shortened names it might not be worth to go with another convention there.



回答2:

from math import sin, sqrt
# Then you can just do
sqrt(4)


回答3:

You can import like this:

>>> from math import sqrt, sin
>>> sqrt(100)
10.0

From: More on modules

There is a variant of the import statement that imports names from a module directly into the importing module’s symbol table. For example:

>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

There is even a variant to import all names that a module defines which can be useful in the interactive interpreter:

>>> from fibo import *
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.



回答4:

let me add that i consider not only from math import * a case of namespace pollution, but also from math import cos. this is because when you do this on top of module foo, and you then look at the namespace of that module with import foo; print( dir( foo ) ), you will have an item cos in that list. usually, this is not what you want.

so most of the time all my imports look like from math import cos as _cos, the leading underscore being the conventional sigil to indicate a private name. the idea is that an import from another module and a printout, import foo; print( name for name in dir( foo ) if not name.startswith( '_' ) ) will give you exactly those names that were defined in that module as public.

there is one thing to be aware of: from math import cos as _cos; f = lambda x: _cos( x ) is functionally not 100% identical to import math; g = lambda x: math.cos( x ). the difference is that in python, name resolution happens at run time, each time the code is called. with the first import, the name cos is only resolved once; any subsequent changes to the math module will not affect f(). with the second import, cos will be resolved against math each time it is called, so changes to the math module's cos method will propagate into g(). of course, the math module is neither expected to change during runtime, nor is changing a module's method at runtime a particularly recommendable programming technique. on the other hand, a module is just an object like everything else in python, so it pays off to always be aware what happens under the hood.

normally, from math import cos as _cos; f = lambda x: _cos( x ) is what you want, and it is also a bit faster than the second form.