Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9063839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:06:10+00:00 2026-06-16T16:06:10+00:00

I have a module that defines a class which instantiates a class from one

  • 0

I have a module that defines a class which instantiates a class from one of two (or more) other modules. Below are a couple of code examples. In the first example, two modules are imported, but only one is used (one per instance of MyIo). In the second example, only the required module is imported. There may be one or more instances of MyIo in a higher level module.

I like that the second example only imports what is used, but I don’t really like that the import is taking place in a ‘code execution’ section.

My questions are:

  1. Which of the examples is a better architectural choice, and why?
  2. Is there a penalty for importing modules that are not eventually
    used?
  3. Are imports in code execution sections in Python considered ‘bad form?’

This example imports both modules, but only uses one…

''' MyIo.py '''

...
...
from DevSerial import Device as DeviceSerial
from DevUSB import Device as DeviceUSB

class MyIo:

  def __init__(self, port)
    if port.lower() == 'usb':
      self.device=DeviceUSB()
    else:
      self.device=DeviceSerial(port)
...
...

The following imports only the module being used…

''' MyIo.py '''

...
...
class MyIo:

  def __init__(self, port)
    if port.lower() == 'usb':
      from DevUSB import Device
      self.device=Device()
    else:
      from DevSerial import Device
      self.device=Device(port)
...
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:06:11+00:00Added an answer on June 16, 2026 at 4:06 pm

    As per PEP 8, all imports should be together at the top of the file. Having them spread throughout the file leads to hard to maintain and debug software.

    The only performance overhead I can think of is at program startup – it has to load more modules. Once the program is running there shouldn’t be any extra overhead.

    To answer your questions:

    1. The former. It is clearly obvious what other files are used, whereas you have to dig through the second to find all the dependencies.
    2. Yes, but only at startup.
    3. Yes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a multi-module maven project that uses a profile that defines a buildnumber-maven-plugin
I have a class in my Python module that frobs widgets. It has several
I have a module file called mymodule.py , which contains the following code: class
I have a two part question: Part 1 I have a module that has
Lets say I have a module called device which contains a class called ConfigureDevice
Currently I have 1 Guice module in my project which defines all bindings. Now
In Ruby, how can I programmatically determine which class/module defines a method being called?
I have a module that looks something like this: def __myFunc(): ... class MyClass(object):
I have some C++ code that defines two classes, A and B. B takes
Let's say that I have a class defined in moduleA.py which I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.