Getting a Generator Object returned when I want the data
<p>I am very very new to Python 3, so please be nice.</p>

<p>I have looked at all the documentation I could find regarding this - there does seem to be a lot, it's just I still cannot figure out what to do.</p>

<p>This is my code:</p>

<pre><code>poly = [[36606.0,53223.0],[37332.0,52224.0],[39043.0,53223.0],
[41603.0,53223.0],[42657.0,53278.0],[43123.0,52060.0],
[44054.0,51156.0],[45806.0,51498.0],[46751.0,53237.0],
[46983.0,54606.0],[45861.0,57057.0],[44971.0,58836.0],
[44054.0,60616.0],[43451.0,58138.0],[41850.0,59179.0],
[40850.0,60370.0],[39906.0,59233.0],[38674.0,59179.0],
[35566.0,56249.0],[37592.0,57536.0]]

def perimeter():
     return (sum((abs((x1 - x0)**2)+abs((y1 - y0)**2))**.5)
                        for ((x0, y0), (x1, y1)) in parts1(poly))

def parts1(poly):
    return poly[0:] + [poly[0]]

print(perimeter())
</code></pre>

<p>It is running <em>without</em> any errors but I am getting a return of:</p>

<p><code>generator object perimeter.locals.genexpr at 0x7f47d8671c50</code></p>

<p>How do I make it so that it gives me the value/answer?
I am really not sure what is going on.</p>

<p>Wrapping an <code>item for item in iterable</code> construct in parentheses makes it a lazy generator expression, which has the appearance you see. Now, the reason it does this instead of giving you an error that you would get from trying to send a non-iterable to <code>sum()</code> is because it doesn't evaluate anything. If you sent this generator expression to <code>list()</code> to evaluate it, you would get an error.</p>

<p>You need to move some parentheses around:</p>

<pre><code>sum((abs((x1 - x0)**2)+abs((y1 - y0)**2))**.5
                        for ((x0, y0), (x1, y1)) in parts1(poly))
</code></pre>

<p>Now you have <code>sum(expression for element in parts1(poly))</code> rather than <code>(sum(expression) for element in parts1(poly))</code>.</p>

<p>Small test:</p>

<pre><code>&gt;&gt;&gt; x1, x0, y1, y0 = 3, 1, 5, 4
&gt;&gt;&gt; sum((abs((x1 - x0)**2)+abs((y1 - y0)**2))**.5
...     for ((x0, y0), (x1, y1)) in [((x0,y0), (x1, y1))])
2.23606797749979
</code></pre>

python python-3.x generator