Imports and dot-notation

Mats Wichmann mats at wichmann.us
Thu Aug 10 10:45:31 EDT 2023


On 8/9/23 17:28, dn via Python-list wrote:

> Side note: Using "...import identifier, ..." does not save storage-space 
> over "import module" (the whole module is imported regardless, IIRC), 
> however it does form an "interface" and thus recommend leaning into the 
> "Interface Segregation Principle", or as our InfoSec brethren would say 
> 'the principle of least privilege'. Accordingly, prefer "from ... import 
> ... as ...".
> 

Attribute lookup has *some* cost.  That is, finding "c" in the local 
namespace is going to be a little quicker than "b.c", where Python finds 
"b" in the local namespace and then finds its "c" attribute; that's then 
a little quicker than "a.b.c", etc.

See all relevant commentary about premature optimisation, spending time 
optimising the wrong things, etc.  but there *are* cases where it 
matters (c.f. a tight loop that will be run many many times)


More information about the Python-list mailing list