views:

504

answers:

4

In C++ you can often drastically improve the readability of your code by careful usage of the "using" keyword, for example:

void foo()
{
    std::vector< std::map <int, std::string> > crazyVector;
    std::cout << crazyVector[0].begin()->first;
}

becomes

void foo()
{
    using namespace std; // limited in scope to foo
    vector< map <int, string> > crazyVector;
    cout << crazyVector[0].begin()->first;
}

Does something similar exist for python, or do I have to fully qualify everything?

I'll add the disclaimer that I know that using has its pitfalls and it should be appropriately limited in scope.

+5  A: 
import X

or

from X import *

or

from X import a, b, c

Where X is the Python module you want to use.

It would be helpful for you to give us a Python code sample that you think needs cleaned up.

Bill the Lizard
+5  A: 

Sure, python's dynamism makes this trivial. If you had a class buried deep in a namespace: foo.bar.baz.blah, you can do:

def foo:
    f = foo.bar.baz.blah
    f1 = f()
Dana
+12  A: 

As Bill said, Python does have the construction

from X import *

but you can also explicitly specify which names you want imported from the module (namespace):

from X import foo, bar, blah

This tends to make the code even more readable/easier to understand, since someone seeing an identifier in the source doesn't need to hunt through all imported modules to see where it comes from. Here's a related question: http://stackoverflow.com/questions/539578/namespace-specification-in-absence-of-ambuguity

EDIT: in response to Pax's comment, I'll mention that you can also write things like

import X.foo

but then you'll need to write

X.foo.moo()

instead of just

foo.moo()

This is not necessarily a bad thing, of course. I usually use a mixture of the from X import y and import X.y forms, whatever I feel makes my code clearest. It's certainly a subjective thing to some extent.

David Zaslavsky
You might want to expand this to include "import X" which then allows you to use X.y things from that module. I sometimes prefer this to reduce polluting the namespace.
paxdiablo
Good call, done ;-)
David Zaslavsky
Why "unlike C++"? Can't you use "using std::string;"?
Constantin
Oh yeah, I forgot about that... I'm not primarily a C++ programmer. Python's import is still a little more powerful though, because you can import individual functions and variables, which I don't think is possible in C++.
David Zaslavsky
David, it's possible in c++ too. (using a "using declaration"). well. can you explain whether you can use python's "import" within functions too, please?
Johannes Schaub - litb
Generally yes, in Python "import" is a statement that can be used anywhere, say, a function call could be. The wildcard import can only be used at the module level, though.
David Zaslavsky
A: 

Note that

from foo import bar

works even if bar is a module in the foo package. This lets you limit your namespace pollution without having to name each function/class in foo.bar that you might care to use. It also aids readers of your code, because they'll see a call to bar.baz() and have a better idea where baz came from.

ruds