Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for - else vs for elif

i thought that elif: was the shorthand for

else:
     if:

but it's not possible to use

for - elif:

only

for - else: if:

in this code:

for line in source:
    change_next = False
    for dataset,artnr,revision in datasets:
        if dataset in line:
            change_next = True
            print "  **  " + dataset + " found"
            datasets.remove((dataset,artnr,revision))
            break
    else:
        if line.startswith("DstID:"):
            print line.replace("DstID:","").rstrip()

    if change_next and "Partno:" in line:
        destination.write("Partno: " + artnr + "\n")
        print "Partno: " + artnr
    elif change_next and "Revno:" in line:
        destination.write("Revno:" + revision + "\n")
        print "Revno:" + revision
    else:
        destination.write(line)

Thanks for the response so far, my question now is rather: is this the way to do it? if a line does not have any (of the known)datasets in it, then i want to print it if it is a dataset?

like image 391
Viktor Mellgren Avatar asked Jun 11 '12 15:06

Viktor Mellgren


1 Answers

elif: isn't a macro that expands to else: if, it is a syntactical element that is valid only in the context of an if: statement. Ordinarily, else: if would open a new if block; however, elif: doesn't do that.

like image 125
kindall Avatar answered Oct 15 '22 05:10

kindall