Reputation: 998
I am dealing with a package with long namespaces, and I was wondering if there is any way to shorten it. For example, some imports look like
from LongNamespace.EvenLongerNamespace.AnotherOne.Class1 import Class1
from LongNamespace.EvenLongerNamespace.AnotherOne.Class2 import Class2
etc.
What I hoped would work is something like
Short = LongNamespace.EvenLongerNamespace.AnotherOne
from Short.Class1 import Class1
from Short.Class2 import Class2
etc.
but unfortunately that doesn't work. I know that you can do
t = import LongNamespace.EvenLongerNamespace.AnotherOne
t.Class1
But that's not clean, I'd rather keep the from x import y
structure
Upvotes: 2
Views: 147
Reputation: 106455
Class1
and Class2
are attributes of the module AnotherOne
, so you can assign them to variables like this:
from LongNamespace.EvenLongerNamespace import AnotherOne
Class1 = AnotherOne.Class1
Class2 = AnotherOne.Class2
You can also import several names in one statement:
from LongNamespace.EvenLongerNamespace.AnotherOne import Class1, Class2
Upvotes: 2