Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

declaring classes from imported modules in python

In python you have to use

import <module>

to import a file with classes.

Imagine the module contains multiple classes

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And now I want to declare an object

x = module.object1Name(parameter1,parmeter2)

How can I declare that object without having to access it through module?
(So like this below)

x = object1Name(parameter1,parameter2)

Also I do not want to use the following because it would mean I would have to do that for every class.

from <module> import <objectName>

>Solution :

You can combine multiple from <module> import <objectName> statements with commas and parentheses.

For example:

from <module> import <objectNameA>, <objectNameB>

or

from <module> import (<objectNameA>, <objectNameB>)

The latter will work across multiple lines (ref).

Using import * from <module> (import everything from <module>) is bad because it risks overriding functions, classes, and variables from other modules, and it violates the Zen of Python "better to be explicit" value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading