简体   繁体   English

类型为“ NoneType”的对象没有len():

[英]object of type 'NoneType' has no len() :

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()

How can I check whether the sp_orders list does not contain any None s? 如何检查sp_orders列表是否不包含任何None

Try this: 尝试这个:

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

This is ensures contact is not None before you try to get its length. 这可以确保在尝试获取长度之前,contact并非为None。 Credit to @Moses Koledoye for pointing out short-circuiting. 感谢@Moses Koledoye指出了短路。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM