---
title: "Discussion on \"Why Your Generator Expression Only Works Once (The Exhaustion Trap)\" | Hashnode"
description: "Discussion on \"Why Your Generator Expression Only Works Once (The Exhaustion Trap)\". Pop quiz. What does this print?\ngen = (x ** 2 for x in range(5))\nprint(sum(gen))\nprint(sum(gen))\n\nIf you said 30 and 30, you just learned something important.\nThe actual output:\n30\n0\nGenerator Expressions Are Single-Use\nA generator expression with pa..."
url: "https://hashnode.com/posts/why-your-generator-expression-only-works-once-the-exhaustion-trap/695c5f85a2876d81054bcacc"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Why Your Generator Expression Only Works Once (The Exhaustion Trap)](https://samuelochaba.hashnode.dev/why-your-generator-expression-only-works-once-the-exhaustion-trap?utm_source=hashnode&utm_medium=feed)

Pop quiz. What does this print? gen = (x \*\* 2 for x in range(5)) print(sum(gen)) print(sum(gen)) If you said 30 and 30, you just learned something important. The actual output: 30 0 Generator Expressions Are Single-Use A generator expression with pa...

Jan 6 · 2 min read · samuelochaba.hashnode.dev  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#learnpython](https://hashnode.com/tag/learnpython)

## Written by

[Samuel Ochaba](https://hashnode.com/@samuelochaba)

@samuelochaba

  
[View profile](https://hashnode.com/@samuelochaba)

Teaching Python for future AI engineers

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
