Is there a conflict of libraries here?

Steve Gronicus at SGA.Ninja
Sat Nov 7 13:26:30 EST 2020


Ok, I think I see a light in the fog.

 

It looks as if I can identify a variable to contain a library.

Import datetime as dt1

 

I guess that I can have a second variable containing that same library:

Import datetime as dt2

 

Should I presume that not doing this is what caused the interference in my
code?

 

In your example:

>>> 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'>}

 

How does your line3 and line2 relate to line1?
It looks as if they relate to dt but it is not specifically stated.  Is it
because they are consecutive lines?

In the list of globals, I see :{}, seems bizarre..

 

Is my dt1 now a variable containing the properties of datetime and they are
listed between  { } and available globally.
Do I then have dt1.datetime and dt2.datetime as separate instances?

Is that how they are identified?  How are they applied?

 

More questions will follow.

Step by step, slowly I turned..

 

Steve

 

------------------------------------------------------------------------

98% of lawyers give the other 2% a bad name.

 

 

-----Original Message-----

From: Python-list <python-list-bounces+gronicus=sga.ninja at python.org> On
Behalf Of Mats Wichmann

Sent: Friday, November 6, 2020 5:57 PM

To: python-list at python.org

Subject: Re: Is there a conflict of libraries here?

 

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



More information about the Python-list mailing list