git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What might cause my sample program to forget that already imported datetime?


On 13/10/2020 06:47, Steve wrote:
> Thank you, those two fixes took care of the problem.

>> At the top of my sample program, I have:
>>
>> import datetime
>> from datetime import *
...
> These are incompatible with each other, so you're going to get issues.
> I'd recommend doing just the first one, and then identifying d2 as
> datetime.datetime.now() instead.


These questions befuddle the mind when first presented, yet the answer 
seems so logical once it becomes hindsight...

Evidence:-
(blank lines added for readability)

dn $ ... python3
Python 3.8.5 (default, Aug 12 2020, 00:00:00)
[GCC 10.2.1 20200723 (Red Hat 10.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> from pprint import pprint as pp

 >>> pp( locals() )
{'__annotations__': {},
  '__builtins__': <module 'builtins' (built-in)>,
  '__doc__': None,
  '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
  '__name__': '__main__',
  '__package__': None,
  '__spec__': None,
  'pp': <function pprint at 0x7fc3d17628b0>}

 >>> import datetime
 >>> pp( locals() )
{'__annotations__': {},
  '__builtins__': <module 'builtins' (built-in)>,
  '__doc__': None,
  '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
  '__name__': '__main__',
  '__package__': None,
  '__spec__': None,
  'datetime': <module 'datetime' from '/usr/lib64/python3.8/datetime.py'>,
  'pp': <function pprint at 0x7fc3d17628b0>}

 >>> from datetime import *
 >>> pp( locals() )
{'MAXYEAR': 9999,
  'MINYEAR': 1,
  '__annotations__': {},
  '__builtins__': <module 'builtins' (built-in)>,
  '__doc__': None,
  '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
  '__name__': '__main__',
  '__package__': None,
  '__spec__': None,
  'date': <class 'datetime.date'>,
  'datetime': <class 'datetime.datetime'>,
  'datetime_CAPI': <capsule object "datetime.datetime_CAPI" at 
0x7fc3d1626ea0>,
  'pp': <function pprint at 0x7fc3d17628b0>,
  'sys': <module 'sys' (built-in)>,
  'time': <class 'datetime.time'>,
  'timedelta': <class 'datetime.timedelta'>,
  'timezone': <class 'datetime.timezone'>,
  'tzinfo': <class 'datetime.tzinfo'>}

Apologies for the awkward email word-wrap.
Note the (absence and then) changing entry for 'datetime'.
-- 
Regards =dn