You want to make use of Python functions. This allows you to make dynamic tasks and allows for code re-usability.
If you are wanting to make an input optional, say related to your question Calculating geometry for feature class with x/y and Lat/Long values in different projections? you could do this:
def getLatLong(fc, lat_field, long_field):
"""add lat/long fields
fc -- path to feature class or shapefile
lat_field -- latitude field to populate
long_field -- longtitude field to populate
# add fields if they don't exist
fieldList = [f.name for f in arcpy.ListFields(fc)]
for f in [lat_field, long_field]:
if f not in fieldList:
arcpy.management.AddField(fc, f, 'DOUBLE')
# populate lat/long fields
wgs = arcpy.SpatialReference(4326)
fields = ['SHAPE@XY', lat_field, long_field]
with arcpy.da.UpdateCursor(fc, fields, spatial_reference=wgs) as rows:
for row in rows:
row[1:] = row
if __name__ == '__main__':
fc = r'C:\TEMP\UofM_cc.shp'
getLatLong(fc, 'Latitude', 'Longitude')
Because this is a Python function, you can pass any point feature class in you want and it will do the coordinate conversion.
This could also be used for a script tool where you could pass in parameters like this:
getLatLong(*[arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())])
And thanks to Paul for mentioning the
spatial_reference option for the update cursor. I always forget about that!