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 7637119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:51:38+00:00 2026-05-31T07:51:38+00:00

I was wondering if passing module objects to a class or object which requires

  • 0

I was wondering if passing module objects to a class or object which requires it rather than using import might not be a bad idea, as it avoids hard dependencies on those modules.

I was wondering if someone more aware of the Python Zen than I might be able to explain why this is a terrible/not terrible idea?

  • 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-05-31T07:51:39+00:00Added an answer on May 31, 2026 at 7:51 am

    What you’re talking about is called dependency injection and is considered a good practice for making your code testable. I don’t think there’s anything about Python that would make it unPythonic or a bad practice.

    There are other ways you could do it in Python, for example by importing different modules depending on some kind of flag you pass in:

    class Foo(object):
        def __init__(self, testing=False):
            if testing:
                import module_test as module
            else:
                import module
            self.module = module
    

    But passing a reference to the module you wish to use is more flexible, separates concerns better, and is no less Pythonic than passing a reference to a class or instance (or string or integer) you wish to use.

    For the ordinary (non-test) use case, you can use a default argument value:

    class Foo(object):
        def __init__(self, module=None):
            if not module:
                import module
            self.module = module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all I was wondering if is it possible to passing an object using
I was wondering what is the best practice re. passing (another class) amongst two
I'm just wondering why passing a System.Collections.Generic.List<string> into this function test(ICollection<object> t) will not
I'm currently wondering if there is a better solution than passing this scope to
I found myself passing InputStream/OutputStream objects around my application modules. I'm wondering if it's
I am using AsyncTask and wondering what are the implications of passing the arguments
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
gcc 4.4.4 c89 I am just wondering is it worth passing a const into
Bit of a dumb question, but I'm wondering what the accepted way of passing
Is there an existing python module that can be used to detect which distro

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.