Is there a conflict of libraries here?

Chris Angelico rosuav at gmail.com
Sun Nov 8 09:56:41 EST 2020


On Mon, Nov 9, 2020 at 1:11 AM Terry Reedy <tjreedy at udel.edu> wrote:
>
> On 11/6/2020 5:05 PM, Steve wrote:
> > "Right, because the name "datetime" points to the class datetime in the
> > module datetime.
>
> A module containing an object with the same name as the module is a real
> pain, a constant mental papercut.  I consider datetime.datetime to be a
> design mistake*.  You are the 2nd person in about a month to post the
> same resulting code problem.
>
> * Either the class should have been 'Datetime', capitalized like classes
> in modules other that builtins generally should be, or the module should
> have been given a different name.  I personally would always rename the
> module when imported.
>

Yes, it's annoying, but it's only annoying when code on the internet
suggests "from datetime import *", which IMO is the real mental
papercut. If everything was written assuming "import datetime",
there'd be only a very minor confusion.

So, my suggestion to anyone who's struggling with datetime is: Don't
ever write "from datetime import *" or "from datetime import
datetime". Your code may break in the short term, but if it does, it's
a solvable break.

ChrisA


More information about the Python-list mailing list