Best practices on python conditionals [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
When it comes to best practices on conditionals, which of the following examples is recommended?
def sum(arg1,arg2):
if arg1>3:
return
else:
return arg1+agr2
or
def sum(arg1,arg2):
if arg1<3:
return arg1+agr2
else:
return
Thanks in advance!

Consider using a ternary expression:
def sum(arg1, arg2):
return arg1 + arg2 if arg1 < 3 else None
As an addendum, if one of the cases is unexpected or undesirable, I like to follow the guard pattern, which involves checking for these cases first before performing your normal logic.
For example,
def safe_divide(a, b):
# Check preconditions at top of function definition
if b == 0:
return None
# Checks passed, perform normal logic
return a / b

Related

What is the most pythonic way to check if a variable is not None? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
When I want to check if an object x is not None, I can either use
if x:
do_something()
or
if x is not None:
do_something()
The first variant feels more pythonic but I think the second variant is easier to read. What is the consensus here?
The two have a different meaning, the first one won't be triggered by most falsy objects ('', False, 0, etc.), the second will. So the logic is different. If you really care about not being None, use the second one.
x = False
if x:
print('one')
if x is not None:
print('two')
output:
two

How can simply this function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can I simply this function:
def fun1(param, value, send):
if send == True:
fun2(**{param:value})
else:
fun2()
You could put the if...else inside the function argument:
def fun1(param, value, send):
return fun2(**({param:value} if send else {}))
Another way to write this function is as follows:
def fun1(param, value, send):
fun2(**{param:value}) if send == True else fun2()
The syntax is:
a if condition_meets else b

multiple returns vs return variable in python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am a beginner programmer using python and I wonder what the best practice is for returns in a function. I have a lot of conditions, but as soon as one of them is true, I want to kill the function and return a bool value.
say, we have:
a)
def foo():
if condition1:
return True
if condition2:
return True
if condition3:
return True
return False
b)
def foo():
bar = False
if condition1:
bar = True
elif condition2:
bar = True
elif condition3:
bar = True
return bar
Is one way better than another? Why? Or is this some utter junk and should be implemented in a completly different way? Is it different in other languages than in python (or, is there a "pythonic" way)?
Thank you all in advance for your answers.
BTW, is there a tag for best practice, or something like that?
The first version is more Pythonic in my opinion, it could be written like this instead though:
return condition1 or condition2 or condition3
Both a.) and b.) are valid. For languages with resource management like C, or debugging and wanting to break in only one place, the preference is b.). See https://softwareengineering.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from.
For Python and other languages it is standard to use either. See Should a function have only one return statement?
You don't use the "bar" variable and you don't need to create it.
Other ways:
def f():
return condition1 or condition2 or condition3
def f2():
return any([condition1, condition2, condition3])

Best structure for if statement in Python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I was wondering what is the recommended way or the best way for optimization between the two different functions.
The first one (that I would personally choose):
def myFunction1(aParameter):
if aParameter == 0:
result = 42
elif aParameter == 1:
result = 0
else:
result = None
return result
The second one:
def myFunction2(aParameter):
if aParameter == 0:
return 42
if aParameter == 1:
return 0
return None
If I'm not mistaken, the behaviour of both functions should be the same, so which one would you recommend?
Thank you in advance! :)
Edit:
Thank you for your quick answers. I understand that this question may be opinion-based.
I was just wondering if there is any PEP recommendation about this or one of the two ways which is seen really more often than the other. Furthermore, about performance, I wanted to know if bytecode generated is the same or not.
If it's just the three paths, either way is probably fine. The generally accepted pythonic way when you start having more paths (which would be handled by a switch statement in other languages) is typically with a hash.
def myFunction1(aParameter):
values = {0:42,1:0}
return values.get(aParameter,None)
I prefer the second one because it should be faster, it's cleaner, and easier to read and debug.

Is "retval" good python style? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a strong reason to use a retval variable vs just returning a calculation?
def add(self, x, y):
return x + y
versus
def add(self, x, y):
retval = x + y
return retval
I feel like I usually see retval (or some other named variable) in code examples but it seems like a (small) waste to me.
In this example it won't make a difference but in longer functions it can be beneficial (for subjective reasons) to have one result or retval variable and only return that value at the end. This can make the code easier to understand (provided it is structured well) by only having one return location.
That being said, it depends on the developer's preferences and in some functions multiple return locations are equally readable.
The only reason to use it is when you want to use the value before you return it. for example, printing it before you return.

Categories