Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python have something like C++'s using keyword?

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.

like image 763
Doug T. Avatar asked Feb 15 '09 01:02

Doug T.


1 Answers

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()
like image 61
Dana Avatar answered Oct 21 '22 19:10

Dana