r/Python • • Oct 08 '26

Discussion Runtime overhead of wildcard import

Assuming a large project which might have say fifty variables in Constants.py is there likely to be any runtime overhead in having from Constants import * at the beginning of each module? As far as I know this creates entries for each of those variables in the current local namespace.

I know it could be done as import constants as x which after the first import then has virtually no overhead, but that then means referring to all constants as x.const1...

18 Upvotes

19 comments sorted by

View all comments

38

u/shibbypwn Oct 08 '26

The objects are being created in memory either way - so the only thing you save with importing the module itself is fewer references in the local namespace. 

But those references add negligible space complexity - you’d need a comically large amount of variables before there was any noticeable difference in performance. 

33

u/[deleted] Oct 09 '26

[removed] — view removed comment

10

u/turbothy It works on my machine Oct 09 '26

I don't disagree from X import * is bad, but

  Someone new to the codebase sees 'from Constants import * and has zero clue where 'MAX_RETRIES` came from without grepping the whole project.

Is this new person looking at a paper printout of the code? In any half-decent IDE you can jump to the definition of any symbol, for example by Ctrl-clicking it.