Lets say I have a variable name = "Jack". I can check it like this
if name:
return name
else:
name = None
return name
Is there any way to handle it in one line like:
new_name = name if not return none
Which simply return the variable value if available and if not then it should return None
Yes, you could do it in one line as shown in my comment:
return name if name else None
Or even simpler for this specific case (as pointed out by Martijn):
return name or None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With