I have this function for labeling layers using arcpy This function works totally fine but I have the field hardcoded into the function
def label(wah):
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == wah:
if lyr.supports("LABELCLASSES"):
for lblClass in lyr.labelClasses:
lblClass.expression = "[field]"
lblClass.showClassLabels = True
lyr.showLabels = True
i want to be able to change the labeling expression for every layer. I have tried this way below, but the labeling does not show up at all.
def label(wah, field):
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == wah:
if lyr.supports("LABELCLASSES"):
for lblClass in lyr.labelClasses:
lblClass.expression = "'["+field+"]'"
lblClass.showClassLabels = True
lyr.showLabels = True
Can someone help me fix my code. i am aware many programmers use .format instead of just using string with the + sign. what is the advantage of that?
when i call the function should it be
label("layer", field)
or
label("layer", "field")