On 11/6/20 3:05 PM, Steve wrote:
"Right, because the name "datetime" points to the class datetime in the
module datetime.
The class, unlike the module, has no "datetime" attribute."

Ok, how do I unpoint/repoint a "datetime" to let the two locations work?

"Apologies if you already knew this.  I wasn't sure."

Absolutely did not know any of this.
Libraries are a slow learning process for me....


The Python import command does some stuff behind the scenes, but from the programmer viewpoint, you're just manipulating names in namespaces - making a mapping of a name to the relevant object.


import boo

gives you a name 'boo' in the relevant namespace, which refers to the module object made for boo when it was imported (that's some of the behind the scenes stuff).

A quick interactive sample showing this in action:

>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> import sys
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'sys': <module 'sys' (built-in)>}

here's the case you are running into:

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


If you want to import the datetime class and still have the full datetime module available, you can for example import as a different name - here's a sample of that:

>>> import datetime as dt
>>> from datetime import datetime
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'dt': <module 'datetime' from '/usr/lib64/python3.8/datetime.py'>, 'datetime': <class 'datetime.datetime'>}

Does this make it clearer?



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to