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.
Related
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
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
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 2 years ago.
Improve this question
I have a question about two different ways of writing a piece of code. I want to know whether they are both okay or one is better under some conditions? Basically, is it better to give the variable we want to update to the function or not?
def f1(num):
output.append(num)
output = []
f1(2)
print(output)
and
def f1(num, output):
output.append(num)
output = []
f1(2, output)
print(output)
In the first example, your function works for only adding element to globally defined certain array. And it is not good approach, you cannot use it for another array.
Second one has generic approach which is better. But only one small correction; you have an array named output, and you pass it to your function, but you keep its name same in your function. So, for your function, there are two output one global and one local, better use different names in this case:
output = []
def f1(num, arr):
arr.append(num)
f1(2, output)
print(output)
Please see warning PyCharm shows in same naming case:
Consider avoiding to use the first example where possible: global variables can be very difficult to work with, generating problems you never find easily. Instead, use the second piece of code.
You could also write something like the following code:
output = []
def add(num, listName):
listName.append(num)
return listname
for _ in range(5):
output = add(_, output)
print(output)
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 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
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.