<p>Why is it bad to name a variable <code>id</code> in Python?</p>

[{'answer_id': 77563, 'author': 'ahockley', 'author_id': 8209, 'author_profile': 'https://Stackoverflow.com/users/8209', 'pm_score': -1, 'selected': False, 'text': "<p>Because it's the name of a builtin function.</p>\n"}, {'answer_id': 77600, 'author': 'Toni Ruža', 'author_id': 6267, 'author_profile': 'https://Stackoverflow.com/users/6267', 'pm_score': 3, 'selected': False, 'text': "<p>It's bad to name any variable after a built in function. One of the reasons is because it can be confusing to a reader that doesn't know the name is overridden.</p>\n"}, {'answer_id': 77606, 'author': 'brian buck', 'author_id': 5926, 'author_profile': 'https://Stackoverflow.com/users/5926', 'pm_score': 2, 'selected': False, 'text': '<p><code>id</code> is a built-in function in Python. Assigning a value to <code>id</code> will override the function. It is best to either add a prefix as in <code>some_id</code> or use it in a different capitalization as in <code>ID</code>.</p>\n<p>The built in function takes a single argument and returns an integer for the memory address of the object that you passed (in CPython).</p>\n<pre><code>&gt;&gt;&gt; id(1)\n9787760\n&gt;&gt;&gt; x = 1\n&gt;&gt;&gt; id(x)\n9787760\n</code></pre>\n'}, {'answer_id': 77612, 'author': 'Kevin Little', 'author_id': 14028, 'author_profile': 'https://Stackoverflow.com/users/14028', 'pm_score': 9, 'selected': True, 'text': '<p><code>id()</code> is a fundamental built-in:</p>\n\n<blockquote>\n  <p>Help on built-in function <code>id</code> in module\n  <code>__builtin__</code>:</p>\n  \n  <pre class="lang-none prettyprint-override"><code>id(...)\n\n    id(object) -&gt; integer\n\n    Return the identity of an object.  This is guaranteed to be unique among\n    simultaneously existing objects.  (Hint: it\'s the object\'s memory\n    address.)\n</code></pre>\n</blockquote>\n\n<p>In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.</p>\n'}, {'answer_id': 77925, 'author': 'Sebastian Rittau', 'author_id': 7779, 'author_profile': 'https://Stackoverflow.com/users/7779', 'pm_score': 6, 'selected': False, 'text': "<p>I might say something unpopular here: <code>id()</code> is a rather specialized built-in function that is rarely used in business logic. Therefore I don't see a problem in using it as a variable name in a tight and well-written function, where it's clear that id doesn't mean the built-in function.</p>\n"}, {'answer_id': 78433, 'author': 'Kitty', 'author_id': 14345, 'author_profile': 'https://Stackoverflow.com/users/14345', 'pm_score': -1, 'selected': False, 'text': "<p>Because python is a dynamic language, it's not usually a good idea to give a variable and a function the same name.  id() is a function in python, so it's recommend not to use a variable named id.  Bearing that in mind, that applies to all functions that you might use... a variable shouldn't have the same name as a function.</p>\n"}, {'answer_id': 79198, 'author': 'Nathan Shively-Sanders', 'author_id': 7851, 'author_profile': 'https://Stackoverflow.com/users/7851', 'pm_score': 6, 'selected': False, 'text': '<p><a href="https://docs.python.org/library/functions.html#id" rel="noreferrer"><code>id</code></a> is a built-in function that gives the identity of an object (which is also its memory address in CPython). If you name one of your functions <code>id</code>, you will have to say <a href="https://docs.python.org/library/builtins.html" rel="noreferrer"><code>builtins.id</code></a> to get the original (or <code>__builtins__.id</code> in CPython). Renaming <code>id</code> globally is confusing in anything but a small script.</p>\n<p>However, reusing built-in names as variables isn\'t all that bad as long as the use is local. Python has a <em>lot</em> of built-in functions that (1) have common names and (2) you will not use much anyway. Using these as local variables or as members of an object is OK because it\'s obvious from context what you\'re doing:</p>\n<p>Example:</p>\n<pre><code>def numbered(filename):\n    with open(filename) as file:\n        for i, input in enumerate(file):\n            print(&quot;%s:\\t%s&quot; % (i, input), end=\'\')\n</code></pre>\n<p>Some built-ins with tempting names:</p>\n<ul>\n<li><code>id</code></li>\n<li><code>file</code></li>\n<li><code>list</code>, <code>dict</code></li>\n<li><code>map</code></li>\n<li><code>all</code>, <code>any</code></li>\n<li><code>complex</code>, <code>int</code></li>\n<li><code>dir</code></li>\n<li><code>input</code></li>\n<li><code>slice</code></li>\n<li><code>buffer</code></li>\n<li><code>sum</code></li>\n<li><code>min</code>, <code>max</code></li>\n<li><code>object</code></li>\n</ul>\n'}, {'answer_id': 28091085, 'author': 'DavidRR', 'author_id': 1497596, 'author_profile': 'https://Stackoverflow.com/users/1497596', 'pm_score': 7, 'selected': False, 'text': '<p>In <strong>PEP 8 - Style Guide for Python Code</strong>, the following guidance appears in the section <a href="https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles" rel="noreferrer">Descriptive: Naming Styles </a>:</p>\n\n<blockquote>\n  <ul>\n  <li><p><code>single_trailing_underscore_</code> : used by convention to avoid conflicts\n  with Python keyword, e.g.</p>\n  \n  <p><code>Tkinter.Toplevel(master, class_=\'ClassName\')</code></p></li>\n  </ul>\n</blockquote>\n\n<p>So, to answer the question, an example that applies this guideline is:</p>\n\n<pre><code>id_ = 42\n</code></pre>\n\n<p>Including the trailing underscore in the variable name makes the intent clear (to those familiar with the guidance in PEP 8).</p>\n'}, {'answer_id': 62973285, 'author': 'wjandrea', 'author_id': 4518341, 'author_profile': 'https://Stackoverflow.com/users/4518341', 'pm_score': 3, 'selected': False, 'text': '<p>Others have mentioned that it\'s confusing, but I want to expand on <strong>why</strong>. Here\'s an example, based on a true story. Basically, I wrote a class that takes an <code>id</code> parameter but then tried to use the builtin <code>id</code> later.</p>\n<pre><code>class Employee:\n    def __init__(self, name, id):\n        &quot;&quot;&quot;Create employee, with their name and badge id.&quot;&quot;&quot;\n        self.name = name\n        self.id = id\n        # ... lots more code, making you forget about the parameter names\n        print(\'Created\', type(self).__name__, repr(name), \'at\', hex(id(self)))\n\ntay = Employee(\'Taylor Swift\', 1985)\n</code></pre>\n<p>Expected output:</p>\n<pre class="lang-none prettyprint-override"><code>Created Employee \'Taylor Swift\' at 0x7efde30ae910\n</code></pre>\n<p>Actual output:</p>\n<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):\n  File &quot;company.py&quot;, line 9, in &lt;module&gt;\n    tay = Employee(\'Taylor Swift\', 1985)\n  File &quot;company.py&quot;, line 7, in __init__\n    print(\'Created\', type(self).__name__, repr(name), \'at\', hex(id(self)))\nTypeError: \'int\' object is not callable\n</code></pre>\n<blockquote>\n<p><em>Huh? Where am I trying to call an int? Those are all builtins...</em></p>\n</blockquote>\n<p>If I had named it <code>badge_id</code> or <code>id_</code>, I wouldn\'t have had this problem.</p>\n'}]
['https://Stackoverflow.com/questions/77552', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5926/']