'In Python 3, is there a built in utility to check for namespace overlap between two or more scripts? If not, how could I do this?

Update for clarity: I'm looking for a function that will return information about the namespace of a script (i.e. list all the variable names, function names, class names etc. that are explicitly defined in the script).

Consider the following scenario,

#script 1.py
a = 'spam'
def makeOmlette():
    print('eggs and spam')
    return None

#script 2.py
a = 'bacon'
def makeOmlette():
    print('eggs and bacon')

#Desired Capability:
>>> import someBuiltInModule
>>> someBuiltInModule.BuiltInNameSpaceChecker.compare( scrips = ['folder//script1.py', 'folder//script2.py'])#which would return...

('Scripts are not compatable',
 'global variable "a defined in files script1.py and script2.py',
 'function "makeOmlette" defined in files cript1.py and script2.py',
 'other useful information before you might combine the two...')

The goal would be to check if copying and pasting script 1 and 2 into the same file would cause any namespace overlap.

Yes, I know, I could just have the new script use import statements.

#script 3 - onlyWayIKnow.py
import script_1
import script_2

>>> script_1.a == script_2.a

False

The reason for my inquiry (in case I am thinking about this wrong) is that if I use the aforementioned solution, I would then have to maintain 3 scripts going forward instead of just 1.



Solution 1:[1]

You can't have a namespace collision because you imported the modules, not their names.

import spam
import bacon

spam.make_omelette() # makes a spam omelette
bacon.make_omelette() # makes a bacon omelette

Alternatively, you can import exactly what you need, nothing more, and you can rename it when necessary.

from spam import make_omelette as make_spam_omelette
from bacon import make_omelette as make_bacon_omelette

make_spam_omelette()
make_bacon_omelette()

On the other hand, if you did something like this, you'd have a problem, and it would be your fault:

# BAD
from spam import *
from bacon import *

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kenny Ostrom