简体   繁体   中英

Two return statements for one method? Python

I have a method which should print reports. There are two reports that are printed using the same method but has different conditions. I have given the if-else condition but some reason the else part is not being executed! Kindly help me with the issue

count = 80
a = 20
if a > count:
    return xyz
else:
    return abc

abc and xyz are two different types of reports that I have.

Edit: This is my actual function. In each I'm fetching my records.

            for inv_no in each:
                if inv_no.invoice_date > '2017-06-30':
                    return {
                            'type': 'ir.actions.report.xml',
                            'report_name': 'gst_invoice_print',
                            'datas': datas,
                            }
                else:
                    return {
                            'type': 'ir.actions.report.xml',
                            'report_name': 'invoice_print',
                            'datas': datas,
                            }

I don't understand your problem, because your code works perfectly. This code below works for me:

count = 80
a = 20
def test(a, count):
    if a > count:
        return "xyz"
    else:
        return "abc"
print test(a,count)

it returns "abc"...

I saw your last commend so this is how you should compare dates.

Odoo dates are not comparable until you convert them to datetime objects, so to convert odoo date to datetime object use:

a = datetime.strptime(self.date_field1, "%Y-%m-%d")
b = datetime.strptime(self.date_field1, "%Y-%m-%d")
# where date_field1 and date_field2 are something like this 2017-01-01
# now you can compare a and b
if a < b:
    drink beer
else:
    drink more beer

So now that we know exactly what is going on, Python's return statement actually 'exits' the function not continue it afterwards. What you need is to keep track of all the reports and then return it rather than doing so immediately. A way to do so is with a list or tuple.

I prefer lists as they are mutable for future use but if you need to use a tuple use ( ) instead of [ ] .

reports = []
for inv_no in each:
    if inv_no.invoice_date > '2017-06-30':
        reports.append({
                        'type': 'ir.actions.report.xml',
                        'report_name': 'gst_invoice_print',
                        'datas': datas,
                        })
    else:
        reports.append({
                        'type': 'ir.actions.report.xml',
                        'report_name': 'invoice_print',
                        'datas': datas,
                        })

return reports

This way you can get the appropriate reports for each date. To access the reports later on in the code, you do so with reports["index of list"]["key of dict"]

Well, the code you provided is always going to print the report abc since what you are comparing always returns false (As everyone else had said).

If you do want to retrieve either of the two reports depending on the situation, (In the case of a compare) either one or both values should be mutable. So they need to be inputed by the user, a parameter for the function, or computer inputted (such as a variable provided by the computer. Like time)

Now if the snippet of code you provided represents a portion of your function and assuming the count variable is keeping track of an iteration, you would need to have the return statement after the iteration to make sure everything is accounted for.

for count in list:
    # Do something

if a > count:
    return xyz
else:
    return abc

However, the a variable or list variable (don't use list as a name for a variable, this is just an example) must be mutable. Otherwise the function will always return the same thing.

As a note: If you want more relevant answers, include more code that better represents what you are asking (Such as a function, class, or the areas where input is retrieved and then manipulated). Don't just provide the code where there is the error but also what leads to it.

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