For the following code:
# infile.py
FILE1 = 'file1'
FILE2 = 'file2'
def getfile():
file = FILE1 if 'FILE1' in locals() else FILE2
print(f'{file=}')
if __name__ == '__main__':
getfile()
If I run this on Windows (using Python 3.12.7 or Python 3.13.0), here’s what I see:
PS> .infile.py
file='file2'
If I run this in my VS Code debugger (v1.95.3, Python v2024.20.0, Pylance v2024.12.1, Python Debugger v2024.12.0), here’s what I see:
# Set breakpoint at first line in def getfile function
# Execute following:
file = FILE1 if 'FILE1' in locals() else FILE2
print(file)
file1
My expectation was the result shown in the debugger. It sounds like I’m not quite understanding something with Python scoping. What am I missing?
I’m also curious why I see a difference between what Python does and what the VS Code Python debugger shows. Am I doing something wrong, or is this a bug?