in arcgis proIn ArcGIS Pro you can do this:
cim_version = 'V2'
aprx = arcpy.mp.ArcGISProject(aprx_file)
map0 = aprx.listMaps()[0]
lyr0 = map0.listLayers()[0]
def0 = lyr0.getDefinition(cim_version)
# serviceLayerID will be -1 if "allow assign. unique ids to layers"
# was not check on the Data Frame properties
# in that case, serviceLayerID are in order from 0 -> len(layers) - 1
# & includes GroupLayers (can check lyr0.isGroupLayer, etc)
serviceLayerID = def0.serviceLayerID
cim_version = 'V2'
aprx = arcpy.mp.ArcGISProject(aprx_file)
map0 = aprx.listMaps()[0]
lyr0 = map0.listLayers()[0]
def0 = lyr0.getDefinition(cim_version)
# serviceLayerID will be -1 if "allow assign. unique ids to layers"
# was not check on the Data Frame properties
# in that case, serviceLayerID are in order from 0 -> len(layers) - 1
# & includes GroupLayers (can check lyr0.isGroupLayer, etc)
serviceLayerID = def0.serviceLayerID
 
                