Backstory
A confession, I have used import *
and felt the pain.
Specifically I had from pyspark.sql.functions import *
and then attempted to use a builtin function that had been overridden by that liberal import.
Looking at how many same named functions exist in both, I guess it was only a matter of time before I snagged on a collision:
>>> from pprint import pprint
>>> from pyspark.sql import functions as F
>>> pprint(list(set(dir(F)) & set(dir(__builtin__))))
['sum',
'__package__',
'__doc__',
'round',
'abs',
'min',
'bin',
'filter',
'ascii',
'__spec__',
'hex',
'__loader__',
'slice',
'hash',
'pow',
'max',
'__name__']
Wish
I have experimented with being a little more proactive, to see what has changed in my global namespace, but specifically the builtins are confusing me.
>>> ORIGINAL_GLOBALS = globals().copy()
>>> from pyspark.sql.functions import *
>>> MUTATED_GLOBALS = {key: value for key, value in globals().items() if (key in ORIGINAL_GLOBALS and ORIGINAL_GLOBALS[key] != value)}
>>> print(MUTATED_GLOBALS)
{}
I expected the above to list the above functions that had been hidden by the import *
however instead it is empty.
Questions
- Am I looking in the right place to interrogate the global namespace?
- If so, is there something bespoke happening with
builtins
that I need to take into account? - And if there is something unusual with
builtins
, is there anything else that has the same side-door?
TL;DR
Is there a good, pythonic way for me to detect something having changed in the global namespace?