CreateFeatureClass_management (simple question)

10-10-2018 01:31 PM
MVP Frequent Contributor

I was asked about a seemingly simple line of code in one of my scripts and I didn't have an immediate answer.  After a few pages of g-search, I'm still a bit stumped, but not afraid to ask for the answer!

facFC = arcpy.CreateFeatureclass_management("in_memory", "facFC", "POINT")[0]

What exactly is the [0] at the end of this statement for?

There's examples of having it there and other examples it's not found.


0 Kudos
6 Replies
MVP Legendary Contributor

Create Feature Class—Data Management toolbox | ArcGIS Desktop 

CreateFeatureclass_management (out_path, out_name,
                              {template}, {has_m}, {has_z},
                              {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3},

the [0] is a slice, meaning get the first, which in your case would be the first... first of what is sketchy.

The above is the preferred by me, approach,

out_path…. the location where the featureclass is going

out_name .. the name to give the featureclass

geometry_type… point, line, polygon stuff

template … if you have a featureclass that you want to use for emulation

has_m, has_z - well? do you want them?

spatial_reference - specify it now! or you have to use the define projection tool

the rest... might be important but not critical if saving to a gdb which would have the info anyway.

Note!  there is no ….. blah = CreateFeatureclass

because everything is inside the function

MVP Frequent Contributor

I had a similarly "fuzzy" response when I got the question, I had a hunch that [0] is an index position but not exactly sure to what.

Note!  there is no ….. blah = CreateFeatureclass

I am able to reference facFC var on the next line in order to add a field:

facFC = arcpy.CreateFeatureclass_management("in_memory", "facFC", "POINT")[0]
arcpy.AddField_management(facFC, "NAME", "TEXT", None, None, 40, "", "NULLABLE", "NON_REQUIRED")

Thank you for the response!

0 Kudos
MVP Legendary Contributor

I never liked shortcuts that obscure …

It  is 'ok' but not recommended since putting in the featureclass name as a parameter doesn't hide the fact and relying on slicing might lead you to slice the wrong parameter the next time for the next tool that you use.

Esri Esteemed Contributor

I second what Dan_Patterson  mentioned.  I normally define the output and other variable at the start of the script and use in this case the os module to split the path and name in a path and a name. Something like this:

import arcpy
import os

fc_out = r'C:\some\path\to\outputfgdb.gdb\YourFeatureclass'

ws, fc_name = os.path.split(fc_out)
arcpy.CreateFeatureclass_management(ws, fc_name, etc)

arcpy.AddField_management(fc_out, YourFieldName, etc)
MVP Honored Contributor

I believe the [0] indicates the first item in the Result object returned from the tool, which happens to be the new feature class name.

Using tools in Python—Geoprocessing and Python | ArcGIS Desktop 

MVP Esteemed Contributor

As Darren points out, nearly all geoprocessing tools return Result—Help | ArcGIS Desktop objects, which explains the ArcGIS/ArcPy side of your question.

For the Python side of your question, Python classes can emulate container objects by implementing certain special methods:  3. Data model - Emulating Container Types— Python 2.7.15 documentation.  Support for the indexing and slicing operator, i.e., [], is implemented using the object.__getitem__ special method.  For the ArcPy Result object, the __getitem__ special method either points to Result.getOutput() or implements the same code.

In short, Result[0] is the same as calling Result.getOutput(0) .