0

I thought I understood what "import *" did and its potential dangers, but obviously not.

I've got:

foo.py:

from datetime import datetime
from bar import *
print(datetime.now())

bar.py:

import datetime

The result of running foo.py is an exception:

AttributeError: module 'datetime' has no attribute 'now'

datetime is a module, but datetime.datetime is a type. from datetime import datetime makes it so that datetime in foo.py refers to the type, but the subsequent from bar import * somehow makes it again refer to the module.

Removing the from bar import * makes the exception go away.

But why does from bar import * pollute my namespace with the module datetime? datetime is a module imported in bar, but it isn't defined there. What am I missing?

asked Jan 17, 2019 at 0:05

1 Answer 1

4

The bar module does define a name datetime. The statement

import datetime

creates a module-level datetime variable in the bar module and binds the variable to the datetime module. import * picks up this name the same way it picks up other names.

import * doesn't care about where objects were created. It doesn't care that the datetime module itself comes from some other file. A datetime name exists in bar, so that name gets imported.

answered Jan 17, 2019 at 0:09
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation; it makes sense. I guess I thought that modules from imports were somehow not picked up from an import *, but with your explanation, I guess that would be even weirder.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.