How make your module substitute a python stdlib module.

Antoon Pardon antoon.pardon at vub.be
Tue Dec 27 07:04:53 EST 2022



Op 27/12/2022 om 12:28 schreef Chris Angelico:
> On Tue, 27 Dec 2022 at 22:13, Antoon Pardon<antoon.pardon at vub.be>  wrote:
>>
>>
>> Op 27/12/2022 om 11:37 schreef Chris Angelico:
>>> On Tue, 27 Dec 2022 at 21:29, Antoon Pardon<antoon.pardon at vub.be>  wrote:
>>>> OK, I am writing an alternative for the threading module. What I would
>>>> like to know is how I can get some library modules call my alternative
>>>> instead of the threading module.
>>>>
>>>> For instance there is the logging module, it can log the thread name. So
>>>> I would like to know how I can get the logging module to call the
>>>> function from my module to get the current_thread, instead of it calling
>>>> "current_thread" from the threading module.
>>> Easy: make sure your module is called "threading.py" and is earlier in
>>> the path than the standard library. In fact, it's so easy that people
>>> do it unintentionally all the time... Generally, the current directory
>>> (or the script directory) is the first entry in sys.path, so that's a
>>> good place to put it.
>> Well I had hope for a somewhat more selective solution. The intention is
>> to have a number of modules collected in a package where this module is
>> one of and the package is available via the "installed" search path.
>>
>> So the programmer should just be able to choose to write his code with
>> either:
>>
>>       from threading import Thread
>>
>> or
>>
>>       from QYZlib.threaders import Thread
>>
> How do you intend to distinguish one from the other? How should the
> logging module know which threading module to use?

That is my question! How can I get the logging module to use my module.I was hoping the logging module would allow some kind of dependency 
injection, so you can tell it what threading module to use. An other 
option might be to manipulate sys.modules. -- Antoon Pardon


More information about the Python-list mailing list