<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Why does &amp;quot;for point in part:&amp;quot; work in a standalone ArcPy script, but not in the Field Calculator? in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146772#M3473</link>
    <description>&lt;P&gt;If you feel/believe a reply answers your question, please mark it as the solution to let others know it answered the question for you.&lt;/P&gt;</description>
    <pubDate>Tue, 22 Feb 2022 23:50:22 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2022-02-22T23:50:22Z</dc:date>
    <item>
      <title>Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator?</title>
      <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146351#M3466</link>
      <description>&lt;P&gt;&lt;EM&gt;ArcMap 10.7.1 — Oracle 18c SDE.St_Geometry — Polyline FC&lt;/EM&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;I have an ArcPy function that works as expected in a standalone ArcPy script (PyScripter):&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;new_shape&lt;/SPAN&gt;(&lt;SPAN class=""&gt;geom&lt;/SPAN&gt;):
    spatial_reference =  geom.spatialReference
    parts = arcpy.Array()
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; i &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(geom.partCount):
        part = geom.getPart(i)
        points = arcpy.Array()
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; point &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
            point.M = geom.measureOnLine(point)
            points.append(point)
        parts.append(points)
    &lt;SPAN class=""&gt;return&lt;/SPAN&gt; arcpy.Polyline(parts, spatial_reference)&lt;/PRE&gt;&lt;P&gt;That function works correctly, including the following for loop. It returns a reconstructed shape to the main ArcPy Script.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;for&lt;/SPAN&gt; point &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
    point.M = geom.measureOnLine(point)
    points.append(point)&lt;/PRE&gt;&lt;HR /&gt;&lt;P&gt;However, when I use that same function in the Field Calculator, the script runs without errors, but it&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;returns an empty shape.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The problem seems to be the for loop:&lt;SPAN&gt;&amp;nbsp;&lt;EM&gt;&lt;U&gt;for point in part:&lt;/U&gt;&lt;/EM&gt;&lt;/SPAN&gt;. When I debug the code, I can see that the script doesn't enter the for loop.&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;I&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;was&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;able to work around the issue by using a different style of for loop and the&lt;SPAN&gt;&amp;nbsp;&lt;U&gt;&lt;EM&gt;.getObject()&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://desktop.arcgis.com/en/arcmap/10.7/analyze/arcpy-classes/array.htm" target="_blank" rel="nofollow noopener noreferrer"&gt;array property&lt;/A&gt;. Now, the script enters the for loop and constructs the shape from the points, as expected:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;for&lt;/SPAN&gt; j &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(part.count):
    point = part.getObject(j)
    point.M = geom.measureOnLine(point)
    points.append(point)&lt;/PRE&gt;&lt;HR /&gt;&lt;P&gt;Question:&lt;/P&gt;&lt;P&gt;Why does&amp;nbsp;&lt;U&gt;&lt;EM&gt;for point in part:&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;work in a standalone ArcPy script, but not in the Field Calculator?&lt;/P&gt;&lt;P&gt;I would like to understand this better — in case I'm doing something wrong.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 06:05:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146351#M3466</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-02-22T06:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator?</title>
      <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146737#M3471</link>
      <description>&lt;P&gt;&lt;EM&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/351335"&gt;@Bud&lt;/a&gt;, you posted this question on GIS StackExchange ( &lt;A href="https://gis.stackexchange.com/questions/423990/why-does-for-point-in-part-work-in-a-standalone-arcpy-script-but-not-in-the/424532#424532" target="_self"&gt;arcmap - Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator? - GIS Stack Exchange&lt;/A&gt; ) , and I just posted an answer there.&amp;nbsp; I will put the answer here as well since I suspect a fair number of Esri Community members aren't users of GIS SE.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;The difference in behavior is due to the fact that objects within geoprocessing tools are not the same as objects within ArcPy. The pattern of directly iterating over items in a container,&lt;FONT face="courier new,courier"&gt; for point in part:&lt;/FONT&gt;, is a Python use pattern that is implemented in most ArcPy classes but not in ArcObjects classes being used by geoprocessing tools.&lt;/P&gt;&lt;P&gt;Looking to Esri's &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy/a-quick-tour-of-arcpy.htm" target="_self"&gt;A quick tour of ArcPy -- ArcMap | Documentation&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ArcPy provides access to geoprocessing tools as well as additional functions, classes, and modules that allow you to create simple or complex workflows. Broadly speaking, ArcPy is organized in tools, functions, classes, and modules.&lt;/P&gt;&lt;P&gt;Technically speaking, geoprocessing tools are functions available from arcpy—that is, they are accessed like any other Python function. However, to avoid confusion, a distinction is always made between tool and nontool functions (such as utility functions like ListFeatureClasses()).&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The key is in the second paragraph, i.e., geoprocessing tools are functions available from ArcPy but are not native ArcPy functions. Field Calculator is an example of a geoprocessing tool available through ArcPy as opposed to an ArcPy function that supports Python use patterns. Typically with geoprocessing tools, users either pass basic Python data types (e.g., string) or ArcPy objects that are then converted to geoprocessing objects behind the scenes. Field Calculator is somewhat unique in that users can pass free-form Python code within a Python string for the tool to execute.&lt;/P&gt;&lt;P&gt;In ArcPy, &lt;FONT face="courier new,courier"&gt;arcobjects&lt;/FONT&gt; are created from &lt;FONT face="courier new,courier"&gt;geoprocessing&lt;/FONT&gt; code and then extended to add support for core Python functionality and patterns. For ArcPy &lt;FONT face="courier new,courier"&gt;Array&lt;/FONT&gt;, that includes implementing the Python &lt;FONT face="courier new,courier"&gt;__iter__&lt;/FONT&gt; special method that supports the pattern of directly iterating over items in a container.&amp;nbsp; From &lt;A href="https://docs.python.org/3/reference/datamodel.html#object.__iter__" target="_blank"&gt;3. Data model — Python 3.10.2 documentation&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;object.__iter__(self)&lt;/P&gt;&lt;P&gt;This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;If you look at the &lt;FONT face="courier new,courier"&gt;ArrayMixin&lt;/FONT&gt; class within the &lt;FONT face="courier new,courier"&gt;mixins&lt;/FONT&gt; module, you can see the implementation of the __iter__ special method is similar to the Python code that currently works within Field Calculator.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# represenative code since ArcPy modules are copyrighted
def __iter__(self):
    for index in xrange(len(self)):
        yield conversionObject(self.GetObject(index))&lt;/LI-CODE&gt;&lt;P&gt;You can verify that a geoprocessing &lt;FONT face="courier new,courier"&gt;array&lt;/FONT&gt; does not implement the __iter__ special method while ArcPy &lt;FONT face="courier new,courier"&gt;Array&lt;/FONT&gt; does implement it:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # Create geoprocessing array
&amp;gt;&amp;gt;&amp;gt; gp_arr = arcpy.gp.CreateObject("Array")
&amp;gt;&amp;gt;&amp;gt; type(gp_arr)
&amp;lt;type 'geoprocessing array object'&amp;gt;
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # Create ArcPy Array
&amp;gt;&amp;gt;&amp;gt; arcpy_arr = arcpy.Array()
&amp;gt;&amp;gt;&amp;gt; type(arcpy_arr)
&amp;lt;class 'arcpy.arcobjects.arcobjects.Array'&amp;gt;
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # Check the geoprocessing array for the __iter__ method
&amp;gt;&amp;gt;&amp;gt; gp_arr.__iter__
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
AttributeError: Array: Get attribute __iter__ does not exist
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # Check the ArcPy Array for the __iter__ method
&amp;gt;&amp;gt;&amp;gt; arcpy_arr.__iter__
&amp;lt;bound method Array.__iter__ of &amp;lt;Array []&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 22:35:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146737#M3471</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-02-22T22:35:24Z</dc:date>
    </item>
    <item>
      <title>Re: Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator?</title>
      <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146767#M3472</link>
      <description>&lt;P&gt;Thanks very much. That really helps.&lt;BR /&gt;I posted an ArcGIS Pro Idea about it here:&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/enhance-the-geoprocessing-array-directly-iterate/idi-p/1146763/jump-to/first-unread-message" target="_self"&gt;Enhance the geoprocessing array: Directly iterate over items in a container&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Feel free to suggest improvements to that idea.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 23:36:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146767#M3472</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-02-22T23:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator?</title>
      <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146772#M3473</link>
      <description>&lt;P&gt;If you feel/believe a reply answers your question, please mark it as the solution to let others know it answered the question for you.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 23:50:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1146772#M3473</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-02-22T23:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator?</title>
      <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1147405#M3488</link>
      <description>&lt;P&gt;Related:&lt;/P&gt;&lt;P&gt;&lt;A href="http://Script%20(ArcPy) vs field calculator (Python Parser) behaviour of getPart in ArcGIS for Desktop?" target="_self"&gt;Script (ArcPy) vs field calculator (Python Parser) behaviour of getPart in ArcGIS for Desktop?&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 05:10:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1147405#M3488</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-02-24T05:10:16Z</dc:date>
    </item>
    <item>
      <title>Re: Why does "for point in part:" work in a standalone ArcPy script, but not in the Field Calculator?</title>
      <link>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1153191#M3556</link>
      <description>&lt;P&gt;&lt;BR /&gt;I thought this was interesting:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;"Luckily, arcpy can be accessed inside field calculator, so it's a simple call to&amp;nbsp;&lt;FONT face="courier new,courier" color="#0000FF"&gt;arcpy.Array():&lt;/FONT&gt;"&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;plineM&lt;/SPAN&gt;(&lt;SPAN class=""&gt;shp&lt;/SPAN&gt;):
  &lt;SPAN class=""&gt;for&lt;/SPAN&gt; part &lt;SPAN class=""&gt;in&lt;/SPAN&gt; arcpy.Array(shp.getPart(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;)):
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; p &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
      &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;None&lt;/SPAN&gt; &lt;SPAN class=""&gt;if&lt;/SPAN&gt; p &lt;SPAN class=""&gt;is&lt;/SPAN&gt; &lt;SPAN class=""&gt;None&lt;/SPAN&gt; &lt;SPAN class=""&gt;else&lt;/SPAN&gt; p.X          

plineM(!Shape!)&lt;/PRE&gt;&lt;P&gt;&lt;A href="https://gis.stackexchange.com/a/158787/62572" target="_self"&gt;Script (ArcPy) vs field calculator (Python Parser) behaviour of getPart in ArcGIS for Desktop?&lt;/A&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Mar 2022 03:00:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/why-does-quot-for-point-in-part-quot-work-in-a/m-p/1153191#M3556</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-03-13T03:00:21Z</dc:date>
    </item>
  </channel>
</rss>

