这可能涉及命名空间,但我不明白为什么在一个示例中找不到该名称,但在另一个示例中找到了
This probably involves the namespaces, but I can't figure what why the name is not found in one example, but is found in another
所以我有两个简单的 Python 模块:
test1.py:
def main():
def fmt(*args):
r = ""
for x in args:
r += eval("f'"+x+"'")
return r
a = "alpha"
b = "beta"
d = "delta"
msg = "Hello {d} one, this is {a} {b}"
print(msg)
print(fmt(msg))
if __name__ == "__main__":
main()
和test2.py:
def fmt(*args):
r = ""
for x in args:
r += eval("f'"+x+"'")
return r
a = "alpha"
b = "beta"
d = "delta"
msg = "Hello {d} one, this is {a} {b}"
print(msg)
print(fmt(msg))
所以,基本上是相同的,除了第一个将代码包装在一个函数中,而第二个没有。第一个在执行时给出以下输出:
Hello {d} one, this is {a} {b}
Traceback (most recent call last):
File "test1.py", line 16, in <module>
main()
File "test1.py", line 13, in main
print(fmt(msg))
File "test1.py", line 6, in fmt
r += eval("f'"+x+"'")
File "<string>", line 1, in <module>
NameError: name 'd' is not defined
第二个如我所料:
Hello {d} one, this is {a} {b}
Hello delta one, this is alpha beta
所以第一个认为它不知道 d
这个名字。但是,如果我在 def fmt(*args):
语句之后将 print(dir(d))
插入到 test1.py 中,现在它会找到 d
,然后不知道下一个名称 a
:
Hello {d} one, this is {a} {b}
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Traceback (most recent call last):
File "test1.py", line 17, in <module>
main()
File "test1.py", line 14, in main
print(fmt(msg))
File "test1.py", line 7, in fmt
r += eval("f'"+x+"'")
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
我相信有人明白发生了什么,但我不知所措。
这确实是标识符范围的问题。在你的 test1.py 中,标识符 a
、b
和 d
在函数 [=14= 的范围内是未知的],因为它们既不是全局的(这就是让它们在您的 test2.py 中广为人知的原因),也不是本地的。您可以通过在 fmt
:
中使用 nonlocal
语句来解决此问题
def main():
def fmt(*args):
nonlocal a, b, d # <----------- only added line of code
r = ""
for x in args:
r += eval("f'"+x+"'")
return r
a = "alpha"
b = "beta"
d = "delta"
msg = "Hello {d} one, this is {a} {b}"
print(msg)
print(fmt(msg))
if __name__ == "__main__":
main()
所以我有两个简单的 Python 模块: test1.py:
def main():
def fmt(*args):
r = ""
for x in args:
r += eval("f'"+x+"'")
return r
a = "alpha"
b = "beta"
d = "delta"
msg = "Hello {d} one, this is {a} {b}"
print(msg)
print(fmt(msg))
if __name__ == "__main__":
main()
和test2.py:
def fmt(*args):
r = ""
for x in args:
r += eval("f'"+x+"'")
return r
a = "alpha"
b = "beta"
d = "delta"
msg = "Hello {d} one, this is {a} {b}"
print(msg)
print(fmt(msg))
所以,基本上是相同的,除了第一个将代码包装在一个函数中,而第二个没有。第一个在执行时给出以下输出:
Hello {d} one, this is {a} {b}
Traceback (most recent call last):
File "test1.py", line 16, in <module>
main()
File "test1.py", line 13, in main
print(fmt(msg))
File "test1.py", line 6, in fmt
r += eval("f'"+x+"'")
File "<string>", line 1, in <module>
NameError: name 'd' is not defined
第二个如我所料:
Hello {d} one, this is {a} {b}
Hello delta one, this is alpha beta
所以第一个认为它不知道 d
这个名字。但是,如果我在 def fmt(*args):
语句之后将 print(dir(d))
插入到 test1.py 中,现在它会找到 d
,然后不知道下一个名称 a
:
Hello {d} one, this is {a} {b}
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Traceback (most recent call last):
File "test1.py", line 17, in <module>
main()
File "test1.py", line 14, in main
print(fmt(msg))
File "test1.py", line 7, in fmt
r += eval("f'"+x+"'")
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
我相信有人明白发生了什么,但我不知所措。
这确实是标识符范围的问题。在你的 test1.py 中,标识符 a
、b
和 d
在函数 [=14= 的范围内是未知的],因为它们既不是全局的(这就是让它们在您的 test2.py 中广为人知的原因),也不是本地的。您可以通过在 fmt
:
nonlocal
语句来解决此问题
def main():
def fmt(*args):
nonlocal a, b, d # <----------- only added line of code
r = ""
for x in args:
r += eval("f'"+x+"'")
return r
a = "alpha"
b = "beta"
d = "delta"
msg = "Hello {d} one, this is {a} {b}"
print(msg)
print(fmt(msg))
if __name__ == "__main__":
main()