Python:理解函数顺序
我通常写一些糟糕的代码,但这次我改变了主意,我想理解Python函数 所以我想重写一些过去的剧本。我不明白为什么这段代码不能执行:Python:理解函数顺序,python,json,function,google-maps-api-3,Python,Json,Function,Google Maps Api 3,我通常写一些糟糕的代码,但这次我改变了主意,我想理解Python函数 所以我想重写一些过去的剧本。我不明白为什么这段代码不能执行: import requests # import json def Status_OK(): for SQL_element in response_data1['results']: SQL_Place
import requests #
import json
def Status_OK():
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
def get_place_for_show():
url1 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?types=movie_theater&location=-36.86667,174.76667&radius=25000&key=MyGoogleKey'
response1 = requests.get(url = url1)
response_data1 = response1.json()
if response_data1['status'] == 'OK':
Status_OK()
get_place_for_show()
我的答复是此错误:
Traceback (most recent call last):
File "Test.py", line 16, in <module>
get_place_for_show()
File "Test.py", line 14, in get_place_for_show
Status_OK()
File "Test.py", line 5, in Status_OK
for SQL_element in response_data1['results']:
NameError: name 'response_data1' is not defined
回溯(最近一次呼叫最后一次):
文件“Test.py”,第16行,在
找个地方看演出()
文件“Test.py”,第14行,在get_place_中显示
状态_OK()
文件“Test.py”,第5行,状态为“OK”
对于响应_data1['results']中的SQL_元素:
NameError:未定义名称“response_data1”
什么name错误:未定义名称“response\u data1”?
定义在上面一行
代码应该只打印Google Place_ID的列表,我知道这段代码可以工作,因为我在另一个脚本上使用它,但没有函数该变量是函数的一个局部变量。如果要在多个函数中使用它,可以在函数调用中传递它,也可以在更大的范围内定义它。 您可以看到函数中有哪些可用变量
>>> get_place_for_show.__code__.co_varnames
('url1', 'response1', 'response_data1')
现在让我们看看Status_OK函数中的情况
>>> Status_OK.__code__.co_varnames
('SQL_element', 'SQL_Place_ID')
这就是为什么会出现名称错误异常;函数没有该属性(param、arg、local)。要解决此问题,请执行以下操作:
import requests #
import json
response_data1 = {}
def Status_OK():
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
def get_place_for_show():
url1 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?types=movie_theater&location=-36.86667,174.76667&radius=25000&key=MyGoogleKey'
response1 = requests.get(url = url1)
response_data1 = response1.json()
if response_data1['status'] == 'OK':
Status_OK()
get_place_for_show()
response\u data1
是函数get\u place\u for\u show
中的一个局部变量。仅仅因为您在其内部调用了Status\u OK
,并不意味着它将(或应该)共享get\u place\u for\u show
的范围。试着读一下
现在,如果您希望出现类似于闭包的行为,那么需要在第一个函数中定义另一个函数
def get_place_for_show():
...
response_data1 = response1.json()
def Status_OK():
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
Status_OK()
# Now it won't at least raise an error.
...
话虽如此,这只是为了演示,我在这里没有看到实例化闭包的任何用例。关于标准问题变量占空比的一点理论:
在函数中创建变量时,在执行函数中的最后一条语句后,该变量将被销毁。您需要通过以下方式将var的值传递给函数:
def Status_OK(response_data1):
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
Status_OK(somevar)
这样称呼:
def Status_OK(response_data1):
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
Status_OK(somevar)
您的完整工作代码是:
import requests, json
def Status_OK(response_data1):
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
def get_place_for_show():
url1 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?types=movie_theater&location=-36.86667,174.76667&radius=25000&key=MyGoogleKey'
response1 = requests.get(url = url1)
response_data1 = response1.json()
if response_data['status'] == 'OK':
Status_OK(response_data1)
get_place_for_show()
response\u data1
是get\u place\u for\u show()
中的一个局部变量。它在状态下不可用。\u OK()
。您需要将其作为参数传递给函数。您需要了解变量作用域。该脚本使用了您和@hspandher的一些代码。非常感谢你教我一些全新的东西。这个脚本使用了一些你的代码和@Vinny代码。非常感谢你教了我一些东西new@FrancescoMantovani很高兴,这有帮助。