How do you import classes and methods from another module without retaining the former module namespace?
I am current refactoring some legacy code and am frequently doing imports similar to these.
from legacy_module import ClassA as ClassA from legacy_module import ClassB as ClassB from legacy_module import ClassC as ClassC from legacy_module import methodA as methodA from legacy_module import methodB as methodB
This is done so that the classes can be referenced as ClassA rather than legacy_module.ClassA.
In python, how do you import all of the classes and methods above in a single statement?
from legacy_module import * as *
Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.
To import a module from another path, you first need to import the sys module as well as any other Python modules that you would like to use in your program. The sys module is provided by the Python Standard Library and it provides functions and parameters that are system-specific. The path.
The import statement allows you to import all the functions from a module into your code. Often, though, you'll only want to import a few functions, or just one. If this is the case, you can use the from statement. This lets you import only the exact functions you are going to be using in your code.
Use from legacy_module import *
as your entire import.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With