After accidentally typing too many times import datetimewhen it was really necessary, it was from datetime import datetime, I wondered if it was possible to hack and force the first to take up the last.
That is, to recreate this behavior (in a recently opened interpreter session):
$ python -ic ''
>>> import datetime
>>> datetime(2016, 5, 27)
datetime.datetime(2016, 5, 27, 0, 0)
Approaching the fake "called module" below:
>>> import dt
>>> dt(2016, 5, 27)
datetime.datetime(2016, 5, 27, 0, 0)
What was implemented as follows:
import sys
import datetime
class CallableModule(object):
def __init__(self, thing):
self.thing = thing
def __call__(self, *args, **kwargs):
return self.thing.__call__(*args, **kwargs)
sys.modules['dt'] = CallableModule(datetime.datetime)
However, this will not work if I try to use the file name datetime.pyfor the module, and I have not yet been able to find a hack to get the built-in datetime module when my own file was also named datetime.py,
? - datetime (, , sys.__stdout__ sys.stdout)?
. , - , .