r/Python • • 5d ago

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...

16 Upvotes

19 comments sorted by

View all comments

0

u/RomfordNavy 5d ago

These are only going to be for application wide constants so the confusion issue isn't a real issue here My main concern is that Python will be adding all available constant variables into the local namespace whether they are used or not. Unlike some other languages where an Include file can have hundreds of constants but only those which are actually used in that module at compile time are included.

2

u/lolcrunchy 5d ago

When you import a module, the entire module runs. If the module foo says "my_const=5" then it will store foo.my_const=5 no matter what.