对象类型' NoneType'没有len():

时间:2017-09-21 10:16:12

标签: python django nonetype

def send_ivr_calls(sp_orders, base_url, api_key, extra_data):
    for contact in sp_orders:
        if len(contact) == 10:
            contact = '0'+contact
File "views.py", line 43, in calls if len(contact) == 10:
TypeError: object of type 'NoneType' has no len()

如何检查sp_orders列表中是否包含None个?

2 个答案:

答案 0 :(得分:3)

试试这个:

def send_ivr_calls(sp_orders, base_url, api_key, extra_data):
    for contact in sp_orders:
        if contact and len(contact) == 10:
                contact = '0'+contact

这可以确保在您尝试获取其长度之前,联系不是“无”。感谢@Moses Koledoye指出短路。

答案 1 :(得分:1)

if contact is not None and len(contact) == 10:
    contact = '0'+contact