Код: Выделить всё
# infile.py
FILE1 = 'file1'
FILE2 = 'file2'
def getfile():
# Note: I am doing the equivalent of this with VS Code debugger:
# breakpoint()
file = FILE1 if 'FILE1' in locals() else FILE2
print(f'{file=}')
if __name__ == '__main__':
getfile()
Код: Выделить всё
PS> .\infile.py
file='file2'
Код: Выделить всё
# Uncomment breakpoint at first line in above def getfile function
# Start VS Code in debugging mode (F5)
# VS Code stops at breakpoint
# From VS Code Debug Console, execute following:
file = FILE1 if 'FILE1' in locals() else FILE2
print(f'{file=})
# Output:
file='file1'
Мне также любопытно, почему я вижу разницу между тем, что делает Python, и тем, что показывает отладчик VS Code Python. Я делаю что-то не так или это ошибка?
Исправлено:
Если я сделаю это с помощью pdb, я увижу следующее:
Код: Выделить всё
PS> .\infile
> ...\infile.py(9)getfile()
-> breakpoint()
(Pdb) l
4 FILE1 = 'file1'
5 FILE2 = 'file2'
6
7
8 def getfile():
9 -> breakpoint()
10 file = FILE1 if 'FILE1' in locals() else FILE2
11 # file = FILE1 if 'FILE1' in globals() else FILE2
12 print(f'{file=}')
13
14
(Pdb) 'FILE1' in locals()
False
(Pdb) 'FILE1' in globals()
True
(Pdb)
Подробнее здесь: https://stackoverflow.com/questions/792 ... -scope-loo