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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:50:29+00:00 2026-05-14T14:50:29+00:00

I’ve been reading a lot about python-way lately so my question is How to

  • 0

I’ve been reading a lot about python-way lately so my question is

How to do dependency injection python-way?

I am talking about usual scenarios when, for example, service A needs access to UserService for authorization checks.

  • 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-14T14:50:29+00:00Added an answer on May 14, 2026 at 2:50 pm

    It all depends on the situation. For example, if you use dependency injection for testing purposes — so you can easily mock out something — you can often forgo injection altogether: you can instead mock out the module or class you would otherwise inject:

    subprocess.Popen = some_mock_Popen
    result = subprocess.call(...)
    assert some_mock_popen.result == result
    

    subprocess.call() will call subprocess.Popen(), and we can mock it out without having to inject the dependency in a special way. We can just replace subprocess.Popen directly. (This is just an example; in real life you would do this in a much more robust way.)

    If you use dependency injection for more complex situations, or when mocking whole modules or classes isn’t appropriate (because, for example, you want to only mock out one particular call) then using class attributes or module globals for the dependencies is the usual choice. For example, considering a my_subprocess.py:

    from subprocess import Popen
    
    def my_call(...):
        return Popen(...).communicate()
    

    You can easily replace only the Popen call made by my_call() by assigning to my_subprocess.Popen; it wouldn’t affect any other calls to subprocess.Popen (but it would replace all calls to my_subprocess.Popen, of course.) Similarly, class attributes:

    class MyClass(object):
        Popen = staticmethod(subprocess.Popen)
        def call(self):
            return self.Popen(...).communicate(...)
    

    When using class attributes like this, which is rarely necessary considering the options, you should take care to use staticmethod. If you don’t, and the object you’re inserting is a normal function object or another type of descriptor, like a property, that does something special when retrieved from a class or instance, it would do the wrong thing. Worse, if you used something that right now isn’t a descriptor (like the subprocess.Popen class, in the example) it would work now, but if the object in question changed to a normal function future, it would break confusingly.

    Lastly, there’s just plain callbacks; if you just want to tie a particular instance of a class to a particular service, you can just pass the service (or one or more of the service’s methods) to the class initializer, and have it use that:

    class MyClass(object):
        def __init__(self, authenticate=None, authorize=None):
            if authenticate is None:
                authenticate = default_authenticate
            if authorize is None:
                authorize = default_authorize
            self.authenticate = authenticate
            self.authorize = authorize
        def request(self, user, password, action):
            self.authenticate(user, password)
            self.authorize(user, action)
            self._do_request(action)
    
    ...
    helper = AuthService(...)
    # Pass bound methods to helper.authenticate and helper.authorize to MyClass.
    inst = MyClass(authenticate=helper.authenticate, authorize=helper.authorize)
    inst.request(...)
    

    When setting instance attributes like that, you never have to worry about descriptors firing, so just assigning the functions (or classes or other callables or instances) is fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer try passing in the rails environment task (:cache => :environment)… May 14, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer The capabilities refers to the R binary and its compile-time… May 14, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer Try something like this: $output = array(); $result = -1;… May 14, 2026 at 9:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.