在 Twilio Lookup api 中提取 phone 数字类型的正确 python 查询是什么?为什么?

What is the correct python query to pull phone number type in Twilio Lookup api and why?

基于 Twilio 查找文档 https://www.twilio.com/docs/lookup/api?code-sample=code-carrier-lookup-with-e164-formatted-number&code-language=Python&code-sdk-version=6.x 我假设如下:

phone_number = client.lookups.phone_numbers('+19234567890').fetch(type=['carrier'])

returns:

{
  "caller_name": null,
  "carrier": {
    "error_code": null,
    "mobile_country_code": "123",
    "mobile_network_code": "987",
    "name": "verizon",
    "type": "mobile"
  },
  "country_code": "US",
  "national_format": "(923) 456-7890",
  "phone_number": "+19234567890",
  "add_ons": null,
  "url": "https://lookups.twilio.com/v1/PhoneNumbers/+19234567890?Type=carrier"
}

为什么

print(phone_number['carrier']['type'])

return 一个错误 TypeError: 'PhoneNumberInstance' object is not subscriptable

但是:

print(phone_number.carrier['type'])

正确 return "mobile"

这里是 Twilio 开发人员布道者。

文档显示 API return 的完整 JSON 响应。但是,我们将库构建为 return 个对象,您可以在这些对象上调用常规方法。所以,phone_number 对象 return 在这里:

phone_number = client.lookups.phone_numbers('+19234567890').fetch(type=['carrier'])

根据我们的 API 定义,为 carriercountry_codenational_format.

等定义了方法

另一方面,我认为我们不一定有那个载体对象的定义,所以它被 return编成字典。因此,要查看载体上的字段,您需要使用索引 ([]) 运算符。

您可以检查the definition of the phone_number object on GitHub here