python - trying to understand __self__ == "__main__" -


this question has answer here:

i've never quite gotten down. think if statement allow function run when called , not when module written in imported. take code example:

# finds duplicate values in specified feature class field , populates reords 'y' in field  import arcpy  # enter program inputs def finddupes(infeatureclass, checkfield, updatefield, countrec = 0):     arcpy.da.searchcursor(infeatureclass, [checkfield]) rows:         values = [r[0] r in rows]      arcpy.da.updatecursor(infeatureclass, [checkfield, updatefield]) rows:         row in rows:             if values.count(row[0]) >= 2:                 row[1] = 'y2'                 print "yes"             rows.updaterow(row)             countrec += 1             print countrec if __name__ == '__main__':     fc = raw_input("paste input feature class path arccatolog: ")      fld = raw_input("duplicate field values: ")     = raw_input("update field: ")      finddupes(fc, fld, up) 

the module arcpy irrelevant question; however, way i'm seeing things, if put raw inputs @ top of script still run if imported script function module (import checkfordupes) run upon import statement instead of when function called if __name__ == "__main__" wasn't there. right?

it allows module have different behavior if it's called directly command line vs. being imported other module.

in example, if input statements @ top executed, might not want if module being imported somewhere else , want pass in field values instead of prompting user type on console.


Comments