简体   繁体   中英

Python function, returning False and None

I have this function

def postData():
    data = doPOst()
    if data.result == "success":
       return data
    if data.result == "fail":
       return false

Then i based on return i have to do something like

if ret is None:
   pass # 1
if ret is False:
   pass # 2
else:
   normal

I want to know that will None and False get mixed

If you check for None and False explicitly, then no, they will not get combined.

if ret is None: # entered only on None
    pass
elif ret == False: # entered only on False or 0
    pass
else:
    pass

If you just checked for truthy/falsey values, they would get combined:

if ret:
    pass
else: # entered on None or False
    pass

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