4 ms·
it's an extra function call, but module imports are cached so you're not incurring the actual import cost.
by ledauphin 4y ago
it's an extra function call, but module imports are cached so you're not incurring the actual import cost.
- T-A 4y agoYou can say mylib = None in the global scope and then global mylib if mylib is None: import mylib in your function to avoid the extra function call.
- bobbylarrybobby 4y agoIsn't `is` a function? A cheap function, but still a python function
- int_19h 4y ago"is" is an operator, and better yet, it's not an overloadable operator, so it's about the fastest thing you can do to two values in Python.
- dagmx 4y agoWhy though? import already does the similar check internally. Your global check might actually be more inefficient because it would need to check more dicts instead. E.g first the local dict, then the global and then the modules dict, instead of just the global+modules