简体   繁体   中英

Return multiple values from *args in python

I've already looked at this and this and this , but I'm still confused and unsure where/how to use generators/ yield and splat/ * .

Use case: Sanitize inputs before further manipulating them.

def sanitize_inputs(*args):
    for arg in args:
        if arg == '':
            yield None
        else:
            yield arg

"{}|{}".format(sanitize_inputs('abc',''))

Ideally, sanitize_inputs should return an expanded list so the string substitution will work.

Well... after this was siting in draft state, figured it out, thanks to this

In: "{}|{}".format(*sanitize_inputs('abc',''))
Out: 'abc|None'

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