{
  "id": 337819,
  "title": "gc.collect() vs _ = gc.collect()",
  "url": "/competitions/amex-default-prediction/discussion/337819",
  "author_name": "Ben Lebovitz",
  "post_date": "2022-07-17T20:15:10.778000",
  "votes": 6,
  "comment_count": 6,
  "views": 0,
  "content": "<p>In a lot of notebooks when folks want to release unused dataframes and other objects from memory I see two slightly different lines:</p>\n<ul>\n<li><code>gc.collect()</code></li>\n<li><code>_ = gc.collect()</code></li>\n</ul>\n<p>Just wondering why/when you'd use one.</p>\n<p>Super-Grandmaster <a href=\"https://www.kaggle.com/cdeotte\" target=\"_blank\">@cdeotte</a> uses both at different times in the below notebook…<br>\n<a href=\"https://www.kaggle.com/code/cdeotte/xgboost-starter-0-793\" target=\"_blank\">https://www.kaggle.com/code/cdeotte/xgboost-starter-0-793</a></p>",
  "messages": [
    {
      "id": 1859654,
      "postDate": "2022-07-17T20:21:06.990Z",
      "content": "<p>The code <code>gc.collect()</code> works every time. The reason I use <code>_ = gc.collect()</code> sometimes is to prevent <code>gc</code> from printing text to output when it is the last line in a code cell.</p>\n<p>In general, using the underscore in Python <code>_</code> is a common practice to ignore output of a function. So for example, if a function returns two things like</p>\n<pre><code>def example_function()\n    return 5, 10\n</code></pre>\n<p>And we write <code>x,_ = example_function()</code>, then the variable <code>x</code> receives the first output <code>5</code> and the <code>_</code> is a convention for ignoring the output <code>10</code>.</p>",
      "rawMarkdown": "The code `gc.collect()` works every time. The reason I use `_ = gc.collect()` sometimes is to prevent `gc` from printing text to output when it is the last line in a code cell.\n\nIn general, using the underscore in Python `_` is a common practice to ignore output of a function. So for example, if a function returns two things like\n\n    def example_function()\n        return 5, 10\n\nAnd we write `x,_ = example_function()`, then the variable `x` receives the first output `5` and the `_` is a convention for ignoring the output `10`.",
      "votes": 18,
      "replies": [
        {
          "id": 1859658,
          "postDate": "2022-07-17T20:28:10.043Z",
          "content": "<p>thanks for the super fast reply!</p>\n<p>Do you ever take a day off :)</p>",
          "rawMarkdown": "thanks for the super fast reply!\n\nDo you ever take a day off :)",
          "votes": 4
        },
        {
          "id": 1864401,
          "postDate": "2022-07-21T03:36:14.393Z",
          "content": "<p>Hehe, I found out the reason when I removed all of the second version from my copy of the notebook 😆</p>",
          "rawMarkdown": "Hehe, I found out the reason when I removed all of the second version from my copy of the notebook 😆",
          "votes": 1
        },
        {
          "id": 1864626,
          "postDate": "2022-07-21T07:50:16.770Z",
          "content": "<p>The explanation from <a href=\"https://www.kaggle.com/cdeotte\" target=\"_blank\">@cdeotte</a> about the underscore is incorrect.</p>\n<blockquote>\n  <p>using the underscore in Python _ is a way to send the output of a function to Python garbage collection.</p>\n</blockquote>\n<p><code>_</code> is a normal Python variable name. It has nothing to do with garbage collection whatsoever.</p>\n<p>From <a href=\"https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers\" target=\"_blank\">the documentation</a>:</p>\n<blockquote>\n  <p>[..] _ is a regular identifier. It is often used to name “special” items, but it is not special to Python itself.</p>\n</blockquote>\n<p>as you can see:</p>\n<pre><code>&gt;&gt;&gt; for x, _ in enumerate(range(2,0,-1)):\n...     print(x, _)\n0 2\n1 1\n</code></pre>\n<p>So <code>_ = gc.collect()</code> assigns the return value of <code>gc.collect()</code> to the variable <code>_</code> (preventing a notebook to print it, if in last line, as mentioned above).</p>\n<p>Sorry for the nitpicking, but I find those things important.</p>",
          "rawMarkdown": "The explanation from @cdeotte about the underscore is incorrect.\n\n> using the underscore in Python _ is a way to send the output of a function to Python garbage collection.\n\n`_` is a normal Python variable name. It has nothing to do with garbage collection whatsoever.\n\nFrom [the documentation](https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers):\n\n> [..] _ is a regular identifier. It is often used to name “special” items, but it is not special to Python itself.\n\nas you can see:\n\n```\n>>> for x, _ in enumerate(range(2,0,-1)):\n...     print(x, _)\n0 2\n1 1\n```\n\nSo `_ = gc.collect()` assigns the return value of `gc.collect()` to the variable `_` (preventing a notebook to print it, if in last line, as mentioned above).\n\nSorry for the nitpicking, but I find those things important.",
          "votes": 2
        },
        {
          "id": 1865474,
          "postDate": "2022-07-21T20:42:36.020Z",
          "content": "<p>Thanks for the clarification <a href=\"https://www.kaggle.com/arigion\" target=\"_blank\">@arigion</a> . I updated my post above for correctness.</p>\n<p>FYI, the symbol <code>_</code> is special in Python. After any command, we can use <code>_</code> to access the result of the previous command. For example</p>\n<pre><code>&gt;&gt;&gt; 1 + 2\n3\n&gt;&gt;&gt; _\n3\n&gt;&gt;&gt; 3 + 4\n7\n&gt;&gt;&gt; _\n7\n&gt;&gt;&gt; _ + 2\n9\n</code></pre>\n<p>But if we assign a value to <code>_</code> then it behaves like a normal variable like you say. So </p>\n<pre><code>&gt;&gt;&gt; 1 + 2\n3\n&gt;&gt;&gt; _\n3\n&gt;&gt;&gt; _ = 9\n&gt;&gt;&gt; 3 + 4\n7\n&gt;&gt;&gt; _\n9\n</code></pre>\n<p>That's strange. (I thought after assignment, it would get overwritten with any new command but it does not). So after using it, then it becomes a variable and it will no longer show the last command unless we explicitly delete the variable with <code>del _</code>. Then it returns to being a special character.</p>",
          "rawMarkdown": "Thanks for the clarification @arigion . I updated my post above for correctness.\n\nFYI, the symbol `_` is special in Python. After any command, we can use `_` to access the result of the previous command. For example\n\n    >>> 1 + 2\n    3\n    >>> _\n    3\n    >>> 3 + 4\n    7\n    >>> _\n    7\n    >>> _ + 2\n    9\n\nBut if we assign a value to `_` then it behaves like a normal variable like you say. So \n\n    >>> 1 + 2\n    3\n    >>> _\n    3\n    >>> _ = 9\n    >>> 3 + 4\n    7\n    >>> _\n    9\n\nThat's strange. (I thought after assignment, it would get overwritten with any new command but it does not). So after using it, then it becomes a variable and it will no longer show the last command unless we explicitly delete the variable with `del _`. Then it returns to being a special character.",
          "votes": 1
        }
      ]
    },
    {
      "id": 1859647,
      "postDate": "2022-07-17T20:15:10.780Z",
      "content": "<p>In a lot of notebooks when folks want to release unused dataframes and other objects from memory I see two slightly different lines:</p>\n<ul>\n<li><code>gc.collect()</code></li>\n<li><code>_ = gc.collect()</code></li>\n</ul>\n<p>Just wondering why/when you'd use one.</p>\n<p>Super-Grandmaster <a href=\"https://www.kaggle.com/cdeotte\" target=\"_blank\">@cdeotte</a> uses both at different times in the below notebook…<br>\n<a href=\"https://www.kaggle.com/code/cdeotte/xgboost-starter-0-793\" target=\"_blank\">https://www.kaggle.com/code/cdeotte/xgboost-starter-0-793</a></p>",
      "rawMarkdown": "In a lot of notebooks when folks want to release unused dataframes and other objects from memory I see two slightly different lines:\n- `gc.collect()`\n- `_ = gc.collect()`\n\nJust wondering why/when you'd use one.\n\nSuper-Grandmaster @cdeotte uses both at different times in the below notebook...\nhttps://www.kaggle.com/code/cdeotte/xgboost-starter-0-793\n",
      "votes": 6
    },
    {
      "id": 1863387,
      "postDate": "2022-07-20T10:17:26.833Z",
      "rawMarkdown": "",
      "votes": -1,
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 1859654,
      "author_name": "Chris Deotte",
      "author_url": "",
      "post_date": "2022-07-17T20:21:06.990000",
      "content": "<p>The code <code>gc.collect()</code> works every time. The reason I use <code>_ = gc.collect()</code> sometimes is to prevent <code>gc</code> from printing text to output when it is the last line in a code cell.</p>\n<p>In general, using the underscore in Python <code>_</code> is a common practice to ignore output of a function. So for example, if a function returns two things like</p>\n<pre><code>def example_function()\n    return 5, 10\n</code></pre>\n<p>And we write <code>x,_ = example_function()</code>, then the variable <code>x</code> receives the first output <code>5</code> and the <code>_</code> is a convention for ignoring the output <code>10</code>.</p>",
      "votes": 18,
      "replies": [
        {
          "id": 1859658,
          "author_name": "Ben Lebovitz",
          "author_url": "",
          "post_date": "2022-07-17T20:28:10.043000",
          "content": "<p>thanks for the super fast reply!</p>\n<p>Do you ever take a day off :)</p>",
          "votes": 4,
          "replies": []
        },
        {
          "id": 1864401,
          "author_name": "Robert Hatch",
          "author_url": "",
          "post_date": "2022-07-21T03:36:14.393000",
          "content": "<p>Hehe, I found out the reason when I removed all of the second version from my copy of the notebook 😆</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1864626,
          "author_name": "Arigion",
          "author_url": "",
          "post_date": "2022-07-21T07:50:16.770000",
          "content": "<p>The explanation from <a href=\"https://www.kaggle.com/cdeotte\" target=\"_blank\">@cdeotte</a> about the underscore is incorrect.</p>\n<blockquote>\n  <p>using the underscore in Python _ is a way to send the output of a function to Python garbage collection.</p>\n</blockquote>\n<p><code>_</code> is a normal Python variable name. It has nothing to do with garbage collection whatsoever.</p>\n<p>From <a href=\"https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers\" target=\"_blank\">the documentation</a>:</p>\n<blockquote>\n  <p>[..] _ is a regular identifier. It is often used to name “special” items, but it is not special to Python itself.</p>\n</blockquote>\n<p>as you can see:</p>\n<pre><code>&gt;&gt;&gt; for x, _ in enumerate(range(2,0,-1)):\n...     print(x, _)\n0 2\n1 1\n</code></pre>\n<p>So <code>_ = gc.collect()</code> assigns the return value of <code>gc.collect()</code> to the variable <code>_</code> (preventing a notebook to print it, if in last line, as mentioned above).</p>\n<p>Sorry for the nitpicking, but I find those things important.</p>",
          "votes": 2,
          "replies": []
        },
        {
          "id": 1865474,
          "author_name": "Chris Deotte",
          "author_url": "",
          "post_date": "2022-07-21T20:42:36.020000",
          "content": "<p>Thanks for the clarification <a href=\"https://www.kaggle.com/arigion\" target=\"_blank\">@arigion</a> . I updated my post above for correctness.</p>\n<p>FYI, the symbol <code>_</code> is special in Python. After any command, we can use <code>_</code> to access the result of the previous command. For example</p>\n<pre><code>&gt;&gt;&gt; 1 + 2\n3\n&gt;&gt;&gt; _\n3\n&gt;&gt;&gt; 3 + 4\n7\n&gt;&gt;&gt; _\n7\n&gt;&gt;&gt; _ + 2\n9\n</code></pre>\n<p>But if we assign a value to <code>_</code> then it behaves like a normal variable like you say. So </p>\n<pre><code>&gt;&gt;&gt; 1 + 2\n3\n&gt;&gt;&gt; _\n3\n&gt;&gt;&gt; _ = 9\n&gt;&gt;&gt; 3 + 4\n7\n&gt;&gt;&gt; _\n9\n</code></pre>\n<p>That's strange. (I thought after assignment, it would get overwritten with any new command but it does not). So after using it, then it becomes a variable and it will no longer show the last command unless we explicitly delete the variable with <code>del _</code>. Then it returns to being a special character.</p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 1863387,
      "author_name": "",
      "author_url": "",
      "post_date": "2022-07-20T10:17:26.833000",
      "content": "",
      "votes": -1,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1859654": "The code `gc.collect()` works every time. The reason I use `_ = gc.collect()` sometimes is to prevent `gc` from printing text to output when it is the last line in a code cell.\n\nIn general, using the underscore in Python `_` is a common practice to ignore output of a function. So for example, if a function returns two things like\n\n    def example_function()\n        return 5, 10\n\nAnd we write `x,_ = example_function()`, then the variable `x` receives the first output `5` and the `_` is a convention for ignoring the output `10`.",
    "1859647": "In a lot of notebooks when folks want to release unused dataframes and other objects from memory I see two slightly different lines:\n- `gc.collect()`\n- `_ = gc.collect()`\n\nJust wondering why/when you'd use one.\n\nSuper-Grandmaster @cdeotte uses both at different times in the below notebook...\nhttps://www.kaggle.com/code/cdeotte/xgboost-starter-0-793\n",
    "1863387": ""
  }
}