简体   繁体   中英

Assign variable or return immediately

Hello I was looking for a way to do something like this in Python 3:

def do_something(inp):
    tmp = inp.property or return "Input property is missing"
    tmp_fancy=""
    #
    # Do some fancy stuff 
    #
    return tmp_fancy

To clarify, I want to return from the function immediately in case "input" is None and otherwise proceed. The code above is just an example to show you what I'm looking for.

Basically I'm looking for a shorthand which serves this purpose.

Well you can't have return placed like that since it's a statement. You can use a simple if check to accomplish this:

if inpt is None:
    return "Input is missing"

don't use the name input , though, you'll mask the built-in input inside that function.

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