0

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.

XisUnknown
  • 125
  • 1
  • 1
  • 10
  • 2
    Having multiple smaller files to maintain isn't usually harder than fewer larger files. In fact, it may be easier because you don't have to worry about unintended name clashes in the module. One counter extample, thouth, is that its easier to deploy a single large .py file than a set of msaller ones. In the latter case, you'd end up making a package and creating a setup.py file to make it build and installable. – tdelaney Feb 23 '22 at 17:06

1 Answers1

2

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 *
Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • also make sure you have __name__ == '__main__' before your actual execution in a python script https://stackoverflow.com/questions/419163/what-does-if-name-main-do/419185#419185 – Kenny Ostrom Feb 23 '22 at 17:55
  • The end goal is to combine the two into a single .py script without using any import statements. I'm looking for a built-in function that will return information about the namespace of the script (i.e. list all the variable names, function names, class names etc. that are defined in the script). – XisUnknown Mar 02 '22 at 15:23
  • https://docs.python.org/3/library/inspect.html – Kenny Ostrom Mar 02 '22 at 23:20