简体   繁体   中英

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?

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. Credit to @Moses Koledoye for pointing out short-circuiting.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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