16

What's the best way to get the OBJECTID name of a feature class?

I know you can use "OID@" in a cursor to get the field, but can you get the name from that as well?

I'm writing a script, and need to take into account varying OBJECTID names, like OBJECTID_2, OBJECTID_12, FID, etc. that will be used in a query.

Is using Describe the only way to get the name?

For example:

oid_field = [field.name for field in arcpy.ListFields("FC") if field.type == "OID"]

I'm just wondering if there's another way.

Thanks.

1 Answer 1

30

I would say that using describe is the best

oid_fieldname = arcpy.Describe(fc).OIDFieldName

is quite straightforward. Otherwise, you can directly filter when you use listFields, so you could try

oid_fieldname = arcpy.ListFields(fc,"","OID")[0].name

but this will return an error if there is no oidField (unlikely, but...)

3
  • 1
    I agree it is very straightforward. I've just noticed Describe can be a bit slow. Thanks.
    – ianbroad
    Jul 9, 2014 at 18:04
  • Is ListFields faster than Describe?
    – Learner
    Mar 3, 2020 at 7:12
  • From my experience, no, they are both relatively slow... I've tested on two different medium size) gdb and the results are ~6.4 for Describe and ~7.4 for ListFields (from scratch). If the fc is already in memory, the time is ~1.6 and 2.5, respectively.
    – radouxju
    Mar 3, 2020 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.